一.地图显示
步骤
1.导入sdk:拷贝jar文件于项目目录libs下,将so文件拷贝至armeabi下
2.配置Manifest:拷贝帮助文档
3.配置Activity:
获取控制器 mMapView=(MapView)findViewById(R.id.bmapsView); mMapView.setBuiltInZoomControls(true); //设置启用内置的缩放控件 mMapController =mMapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放 //用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6) mMapController.setZoom(16);//设置地图zoom级别 |
初始化地图 mManager=new BMapManager(getApplication()); mManager.init(key, null); //注意:请在试用setContentView前初始化BMapManager对象,否则会报错 setContentView(R.layout.activity_main); |
|
4.相关类:
BMapManager:地图引擎管理类,提供打开,关闭,初始化,退出百度地图API
MapView:显示地图,提供多种模式如卫星模式,交通模式
MapController:控制器,提供定位到中心点,缩放动画显示地图
GeoPoint:地理坐标
二.地图定位
注:地图sdk与定位sdk分立,定位sdk不需要key即可,详见官网的定位sdk的帮助文档
1.导入sdk---locSDK.jar放入lib-----将liblocSDK.so放入lib\armeabi中
2.配置Manifest
3.相关类
LocationClient:核心类,提供开始,停止定位sdk,定位
LocationClientOption:对定位方式等的设置,通过setLocOption连接
LocationData:我的定位信息
BDLocationListener:监听位置改变
BDLocation:监听的返回的位置
BDNotifyListener:到指定位置时提醒
4.代码
public void locateInit() { // TODO Auto-generated method stub mLocationClient = new LocationClient(this.getApplicationContext());//采用全进程有效的方式 BDLocationListener listener = new MyLocationListener(); //注册位置监听 mLocationClient.registerLocationListener(listener); } //定位方式设置 public void locationOptionSetting() { // TODO Auto-generated method stub LocationClientOption option = new LocationClientOption(); option.disableCache(false); //boolean gpsOpen = option.isOpenGps(); //if(!gpsOpen) option.setOpenGps(true); option.setPoiDistance(500); option.setScanSpan(5000);//5s一更新 option.setPriority(LocationClientOption.NetWorkFirst); option.setAddrType("all");//返回所有地址包含信息 //设置LocationClientOption mLocationClient.setLocOption(option);
} //定位相关 private void locatingPosition() { // TODO Auto-generated method stub mLocationClient.start(); if(mLocationClient!=null&&mLocationClient.isStarted()) mLocationClient.requestLocation();//开始定位 GeoPoint point = new GeoPoint( (int)(latitude*1e6),(int)(longitude*1e6)); |
//实现监听类,此处负责监听位置改变 public class MyLocationListener implements BDLocationListener { StringBuffer geoMsg = null; @Override public void onReceiveLocation(BDLocation location) { // TODO Auto-generated method stub geoMsg = new StringBuffer(); String Addr = null; longitude = location.getLongitude(); latitude = location.getLatitude(); int type = location.getLocType(); if(type == BDLocation.TypeNetWorkLocation){ Addr = location.getAddrStr(); } geoMsg.append("城市:"+location.getCity()+"\n街道:"+location.getStreet()+"\n").append("GPSLocation--经度:"+longitude+"\n").append("纬度:"+latitude+"\n").append("地址:"+Addr+"\n"); locationMsg.setText(geoMsg.toString()); }
@Override public void onReceivePoi(BDLocation location) { // TODO Auto-generated method stub String Addr = null; double longitude = location.getLongitude(); double latitude = location.getLatitude(); int type = location.getLocType(); if(type == BDLocation.TypeNetWorkLocation){ Addr = location.getAddrStr(); } geoMsg.append("城市:"+location.getCity()+"街道:"+location.getStreet()+"\n").append("PoiLocation--经度:"+longitude+"\n").append("纬度:"+latitude+"\n").append("地址:"+Addr+"\n"); locationMsg.setText(geoMsg.toString()); }
} |
三.地图覆盖层
通过MyLocationOverlay获取个人图层,又有定位的位置信息LocationData添加至个人图层,最后由mapView过去图层控制器再通过add就将个人图层添加于地图上
//设置个人位置覆盖图层 MyLocationOverlay overlay = new MyLocationOverlay(mMapView); LocationData data = new LocationData(); data.longitude = this.longitude; data.latitude = this.latitude; data.direction = 2.0f; overlay.setData(data); mMapView.getOverlays().add(overlay);//将覆盖层添加到当前图层上 mMapView.refresh(); mMapController.animateTo(point); mMapController.setCenter(point); |