1.在配置文件中添加百度地图使用的一些相关权限(请求网络等)
成都创新互联公司专注骨干网络服务器租用十年,服务更有保障!服务器租用,内江机房主机托管 成都服务器租用,成都服务器托管,骨干网络带宽,享受低延迟,高速访问。灵活、实现低成本的共享或公网数据中心高速带宽的专属高性能服务器。1 23 4 5 6
7
8 9
2.在配置文件中添加对屏幕的支持
12
3.在layout布局文件中添加显示地图的控件
1 2 6 7 11
12
4.写一个Activity继承MapActivity以下为显示个人坐标以及添加多个覆盖物的代码
1 package com.novaloncn.user.activity.map;
2
3 import java.nio.channels.FileChannel.MapMode;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import android.content.Intent;
8 import android.graphics.drawable.Drawable;
9 import android.location.Location;
10 import android.os.Bundle;
11 import android.util.Log;
12 import android.view.View;
13 import android.widget.Toast;
14
15 import com.baidu.mapapi.BMapManager;
16 import com.baidu.mapapi.GeoPoint;
17 import com.baidu.mapapi.ItemizedOverlay;
18 import com.baidu.mapapi.LocationListener;
19 import com.baidu.mapapi.MKGeneralListener;
20 import com.baidu.mapapi.MapActivity;
21 import com.baidu.mapapi.MapController;
22 import com.baidu.mapapi.MapView;
23 import com.baidu.mapapi.MyLocationOverlay;
24 import com.baidu.mapapi.OverlayItem;
25 import com.novaloncn.user.R;
26 import com.novaloncn.user.activity.ExChange_Map;
27
28
29 public class Android_BaiduMap_test2 extends MapActivity{
30
31 //添加百度地图的相关控件 32 private MapView mapView;
33 private BMapManager bMapManager;//加载地图的引擎
34 //百度地图的key 35 private String keyString="38337A32108C7597D84A9F7E14C1CB6E9844B63A";
36 //在百度地图上添加一些控件,比如是放大或者缩小的控件 37 private MapController mapController;
38 //onResume时注册此listener,onPause时需要Remove 39 private LocationListener mLocationListener=null;//
40 private MyLocationOverlay mLocationOverlay = null; //定位图层 41
42 @Override
43 protected void onCreate(Bundle savedInstanceState) {
44 // TODO Auto-generated method stub 45 super.onCreate(savedInstanceState);
46 setContentView(R.layout.baidumap);
47
48 mapView=(MapView) this.findViewById(R.id.bmapView);
49 bMapManager=new BMapManager(Android_BaiduMap_test2.this);
50
51
52 bMapManager=new BMapManager(Android_BaiduMap_test2.this);
53 //必须加载key 54 bMapManager.init(keyString, new MKGeneralListener() {
55
56 public void onGetPermissionState(int arg0) {
57 // TODO Auto-generated method stub 58 if(arg0==300){
59 Toast.makeText(Android_BaiduMap_test2.this, "输入的Key有错!请核实", 1).show();
60 }
61 }
62
63 public void onGetNetworkState(int arg0) {
64 // TODO Auto-generated method stub 65
66 }
67 });
68
69 initMapActivity(bMapManager);
70 mapView.setBuiltInZoomControls(true);//表示可以设置缩放功能 71
72 mapController=mapView.getController();
73 mapController.setZoom(14);
74
75
76 // //需要定义一个经纬度:上海张江
77 // GeoPoint geoPoint=new GeoPoint((int)(31.514560*1E6), (int)(121.935551*1E6));
78 //
79 // mapController.setCenter(geoPoint);//设置一个中心点
80 //
81 // mapController.setZoom(15);//设置缩放级别是12个级别
82 //
83 // mapView.setBuiltInZoomControls(true);
84 // //设置在缩放动画过程中也显示overlay,默认为不绘制
85 // mapView.setDrawOverlayWhenZooming(true);
86 // // 添加定位图层
87 // mLocationOverlay = new MyLocationOverlay(this, mapView);
88 // mapView.getOverlays().add(mLocationOverlay);
89
90 //显示交通地图
91 // mapView.setTraffic(true);
92 //显示卫星地图
93 // mapView.setSatellite(true);
94
95 // 添加定位图层 96 mLocationOverlay = new MyLocationOverlay(this, mapView);
97 mapView.getOverlays().add(mLocationOverlay);
98
99 // 注册定位事件100 mLocationListener = new LocationListener(){
101
102
103 public void onLocationChanged(Location location) {
104 if (location != null){
105 GeoPoint pt = new GeoPoint((int)(location.getLatitude()*1e6),
106 (int)(location.getLongitude()*1e6));
107 mapView.getController().animateTo(pt);
108 }
109 }
110 };
111
112
113
114
115 }
116 public void daohang(View v){
117 Intent intent=new Intent(Android_BaiduMap_test2.this,ExChange_Map.class);
118 finish();
119 startActivity(intent);
120 }
121 public void myGps(View v){
122 Toast.makeText(Android_BaiduMap_test2.this, "GPS呗点击了", 0).show();
123
124
125
126 mLocationListener=new LocationListener() {
127
128 public void onLocationChanged(Location location) {
129 if(location!=null){
130 GeoPoint pt=new GeoPoint((int)(location.getLatitude()*1e6),
131 (int)(location.getLongitude()*1e6));
132 mapView.getController().animateTo(pt);
133 // mapController.setZoom(4);134 }
135 }
136 };
137 bMapManager.getLocationManager().requestLocationUpdates(mLocationListener);
138
139 mLocationOverlay.enableMyLocation();
140 mapController.zoomIn();
141
142
143 }
144 public void environment(View v){
145 Toast.makeText(Android_BaiduMap_test2.this, "我被点击了", 0).show();
146 Drawable drawable=getResources().getDrawable(R.drawable.iconmarka);
147
148 MyOverLayItem myOverLayItem = new MyOverLayItem(drawable);
149 mapView.getOverlays().add(myOverLayItem);
150 // myOverLayItem.populate();//刷新地图的功能
151 // bMapManager.notifyAll();152 mapController.zoomIn();
153 }
154
155 public class MyOverLayItem extends ItemizedOverlay{
156
157 private List list=new ArrayList();
158 //定义一组坐标,都是以double类型定义
159 // (31.214546*1E6), (int)(121.635574*1E6));160 private double mLat1=31.15443;//表示纬度161 private double mLonl=121.57428;//表示经度162
163 private double mLat2=31.23443;//表示纬度164 private double mLon2=121.657428;//表示经度165
166 private double mLat3=31.22777;//表示纬度167 private double mLon3=121.59000;//表示经度168
169 private double mLat4=31.22477;//表示纬度170 private double mLon4=121.6300;//表示经度171
172 private double mLat5=31.21977;//表示纬度173 private double mLon5=121.634000;//表示经度
174
175
176
177 //用于在地图上标识坐标,用一个图片标注178 public MyOverLayItem(Drawable arg0) {
179 super(arg0);
180 // TODO Auto-generated constructor stub
181 //第一组数据在地图上的标注点182 GeoPoint geoPoint1=new GeoPoint((int)(mLat1*1E6), (int)(mLonl*1E6));
183 //第二组数据在地图上的标注点184 GeoPoint geoPoint2=new GeoPoint((int)(mLat2*1E6), (int)(mLon2*1E6));
185 //第三组数据在地图上的标注点186 GeoPoint geoPoint3=new GeoPoint((int)(mLat3*1E6), (int)(mLon3*1E6));
187 //第四组数据在地图上的标注点188 GeoPoint geoPoint4=new GeoPoint((int)(mLat4*1E6), (int)(mLon4*1E6));
189
190 //第五组数据在地图上的标注点191 GeoPoint geoPoint5=new GeoPoint((int)(mLat5*1E6), (int)(mLon5*1E6));
192
193 list.add(new OverlayItem(geoPoint1, "Ponit1", "Point1"));
194 list.add(new OverlayItem(geoPoint2, "Ponit2", "Point2"));
195 list.add(new OverlayItem(geoPoint3, "Ponit3", "Point3"));
196 list.add(new OverlayItem(geoPoint4, "Ponit4", "位于盛夏路,广兰路"));
197 list.add(new OverlayItem(geoPoint5, "Ponit5", "位与丹桂路,龙东花园"));
198 populate();//刷新地图的功能199
200 }
201
202 //返回指定的list集合中每一个坐标203 @Override
204 protected OverlayItem createItem(int arg0) {
205 // TODO Auto-generated method stub206 return list.get(arg0);
207 }
208
209 @Override
210 public int size() {
211 // TODO Auto-generated method stub212 return list.size();
213 }
214 @Override
215 public boolean onTap(int i) {
216 // TODO Auto-generated method stub217 Log.e("int-------------", i+"");
218 if(i==1){
219 // Intent intent=new Intent(Android_BaiduMap_test2.this,Map2Activity.class);
220 // startActivity(intent);221 }else{
222 Toast.makeText(Android_BaiduMap_test2.this, list.get(i).getSnippet(), 1).show();
223
224 }
225
226 return true;
227 }
228
229 }
230 @Override
231 protected void onDestroy() {
232 // TODO Auto-generated method stub233 super.onDestroy();
234 if(bMapManager!=null){
235 bMapManager.destroy();
236 bMapManager=null;
237 }
238
239 }
240 @Override
241 protected void onResume() {
242 // TODO Auto-generated method stub243 super.onResume();
244 if(bMapManager!=null){
245 // 注册定位事件,定位后将地图移动到定位点246 bMapManager.getLocationManager().requestLocationUpdates(mLocationListener);
247 mLocationOverlay.enableMyLocation();
248 mLocationOverlay.enableCompass(); // 打开指南针249 bMapManager.start();
250 }
251 }
252 @Override
253 protected void onPause() {
254 // TODO Auto-generated method stub255 super.onPause();
256 if(bMapManager!=null){
257 bMapManager.getLocationManager().removeUpdates(mLocationListener);
258 mLocationOverlay.disableMyLocation();
259 mLocationOverlay.disableCompass(); // 关闭指南针260 bMapManager.stop();
261
262 }
263 }
264 @Override
265 protected boolean isRouteDisplayed() {
266 // TODO Auto-generated method stub267 return false;
268 }
269 }
一定要记住 在onPause onDestroy中注销map