Android_百度地图_获得个人的位置并在地图上展现
本页面主要讲述为单个模块抽取出来的使用方法,如果要多个模块整合在一起可以使用百度Demo里面extends MapActivity,下面为个人位置的获取并在地图展现的事项
1.在配置文件中添加百度地图使用的一些相关权限(请求网络等)
1 <!-- 添加百度地图权限 --> 2 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 3 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 4 <uses-permission android:name="android.permission.INTERNET"></uses-permission> 5 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 6 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 7 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> 8 <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> 9 <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
2.在配置文件中添加对屏幕的支持
1 <!-- 添加对屏幕的支持 --> 2 <supports-screens android:largeScreens="true" 3 android:normalScreens="false" android:smallScreens="true" 4 android:resizeable="true" android:anyDensity="true"/>
3.在layout布局文件中添加显示地图的控件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 <!-- 添加显示地图的demo --> 7 <com.baidu.mapapi.MapView android:id="@+id/bmapView" 8 android:layout_width="fill_parent" android:layout_height="fill_parent" 9 android:clickable="true" 10 /> 11 12 </LinearLayout>
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<OverlayItem>{ 156 157 private List<OverlayItem> list=new ArrayList<OverlayItem>(); 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 stub 206 return list.get(arg0); 207 } 208 209 @Override 210 public int size() { 211 // TODO Auto-generated method stub 212 return list.size(); 213 } 214 @Override 215 public boolean onTap(int i) { 216 // TODO Auto-generated method stub 217 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 stub 233 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 stub 243 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 stub 255 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 stub 267 return false; 268 } 269 }
一定要记住 在onPause onDestroy中注销map