下面是代码:
分别声明一个MapView和BaiduMap:
private MapView mMapView;
private BaiduMap mBaiduMap;
在SetContentView之前要初始化SDK:
SDKInitializer.initialize(getApplicationContext());
更换定位图标的方法:
mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo); // 自定义图标
// mCurrentMarker =null ; // 默认图标
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
定位模式:
private LocationMode mCurrentMode;
mCurrentMode = LocationMode.NORMAL; // 普通模式
// mCurrentMode = LocationMode.FOLLOWING; // 跟随模式
// mCurrentMode = LocationMode.COMPASS; // 罗盘模式
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
初始化:
// 地图初始化
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setCoorType(“bd09ll”); // 设置坐标类型
option.setScanSpan(5000);
mLocClient.setLocOption(option);
mLocClient.start();
setScanSpan是定位时间间隔(ms),setCoorType坐标类型分为三种:
bd09ll 百度加密经纬度坐标
bd09 百度加密墨卡托坐标
gcj02 国测局加密经纬度坐标
定位SDK监听函数:
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null)
return;
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整资料开源分享
LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
复写onPause,onResume,onDestory:
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
// 退出时销毁定位
mLocClient.stop();
// 关闭定位图层<