集成百度地圖 SDK 後,地圖初始化時自動顯示藍色定位圖標(藍點),業務需求要求隱藏此默認圖標,改用自定義定位標記。
// 初始化 BaiduMap 對象後
BaiduMap mBaiduMap = mMapView.getMap();
// 關閉默認定位圖層
mBaiduMap.setMyLocationEnabled(false); // 關鍵:禁用內置定位圖層
// 如需自定義定位圖標,使用 MyLocationConfiguration
MyLocationConfiguration config = new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.NORMAL, // 定位模式
true, // 是否顯示方向
customMarkerBitmap // 自定義 BitmapDescriptor,null 為默認
);
mBaiduMap.setMyLocationConfiguration(config);
// 不調用 setMyLocationEnabled(true)
// 在定位成功回調中手動添加 Marker
@Override
public void onReceiveLocation(BDLocation location) {
LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
// 移除舊標記
if (currentMarker != null) currentMarker.remove();
// 添加自定義 Marker
MarkerOptions oo = new MarkerOptions()
.position(ll)
.icon(customBitmapDescriptor)
.zIndex(999);
currentMarker = (Marker) mBaiduMap.addOverlay(oo);
// 可選:移動地圖中心
MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 18);
mBaiduMap.animateMapStatus(u);
}
排查代碼中是否存在:
mLocClient.start() 未配合 setMyLocationEnabled(false)onMapClick 等事件中意外啟用定位| SDK 版本 | 關鍵 API 變更 | 建議 |
|---|---|---|
| v7.0+ | MyLocationConfiguration 全面支持自定義 | 優先使用方法一 |
| v5.0-v6.x | 需手動管理 Marker 生命週期 | 方法二較穩定 |
| v4.x 及以下 | API 差異大,建議升級 | 參考官方遷移指南 |
setMyLocationEnabled(false) 禁用內置藍點MyLocationConfiguration 設置自定義圖標LocationDemo 模塊的實現方式