在百度地图android SDK v2.1.1中,定位使用了百度定位SDK,而不是之前版本的locationlistener。
1、先定义一个定位终端
//定义定位终端
mLocClient = new LocationClient(this);//构造新的定位终端
mLocClient.registerLocationListener(myLocationListener);//注册监听函数
LocationClientOption option = new LocationClientOption();//构造定位方式
option.setOpenGps(true); //打开gps
option.setCoorType("bd09ll"); //设置坐标类型,为百度经纬坐标系
option.setLocationNotify(false);
option.setAddrType("all"); //表示返回地址信息
//option.setScanSpan(5000); //设置定时定位的时间间隔 ,为5s
mLocClient.setLocOption(option);//设置定位方式
mLocClient.start(); //启动终端
2、注册一个监听函数,获取定位信息数据
public MyLocationListenner myLocationListener = new MyLocationListenner();
/**
* 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null){
return ;
}
locData.latitude = location.getLatitude();//纬度
locData.longitude = location.getLongitude();//经度
Geo = new GeoPoint((int)(locData.latitude* 1e6),(int)(locData.longitude * 1e6));
initPopupOverLay();//窗体显示
locData.accuracy = location.getRadius(); //精度
locationmsg.setText("我的定位精度为:"+((int)locData.accuracy)+"米");//显示精度
locData.direction = location.getDerect(); //方向角度
mLocationOverlay.setData(locData);
mMapView.refresh();
//Message msg=new Message();
// msg.what=LOCATION_REDAY;
mMapController.animateTo(Geo, mHandler.obtainMessage(LOCATION_REDAY));//图层移动到定位点
mSearch.reverseGeocode(Geo);//发送经纬度,反解析为地址信息
}
public void onReceivePoi(BDLocation poiLocation) {
if (poiLocation == null){
return ;
}
}
}
3、再把获取的数据加到图层上面
// 添加定位图层
mMapView.regMapViewListener(CompusAssistApp.getInstance().mBMapMan, mMapViewListener);//注册地图监听器
mLocationOverlay = new MyLocationOverlay(mMapView);
locData = new LocationData(); //用户位置信息
mLocationOverlay.setData(locData);//将信息加到图层上面
mMapView.getOverlays().add(mLocationOverlay);
mLocationOverlay.enableCompass(); //打开指南针
mMapView.refresh(); //图层刷新
这样定样图层就显示在你的原始图层了,效果如下:
安卓市场:http://apk.hiapk.com/html/2013/05/1468059.html?module=256&info=IWjtVg9cqVJLYg%3D%3D
N多市场:http://www.nduoa.com/apk/detail/553415
360手机助手:http://zhushou.360.cn/search/index/?kw=%E6%A0%A1%E5%9B%AD%E5%B0%8F%E5%8A%A9%E6%89%8B
百度应用:http://as.baidu.com/a/item?docid=3101724&pre=web_am_se
优亿市场(eoe):http://www.eoemarket.com/search/apps/?keyword=%E6%A0%A1%E5%9B%AD%E5%B0%8F%E5%8A%A9%E6%89%8B