Mapbox Android 7.1.1版本部分API弃用无替代方案咨询
Hey there! I’ve dealt with this exact Mapbox migration hurdle in v7.1.1, so let’s walk through how to replace those old marker APIs using the Annotation Plugin you already added.
Replacing addMarker, MarkerOptions, and IconFactory
The old marker-related classes got replaced by the SymbolManager and SymbolOptions from the Annotation Plugin. Here’s how to refactor your code step by step:
First, set up the SymbolManager
You need to initialize this manager after your MapboxMap is ready (usually inside the onMapReady callback):
// Assuming you have references to your MapView and MapboxMap val symbolManager = SymbolManager(mapView, mapboxMap) // Tweak these settings if you want markers to overlap or ignore map labels symbolManager.iconAllowOverlap = true symbolManager.iconIgnorePlacement = true
Then create and add your marker
Replace your old addMarker line with this code:
// Use the SymbolManager's built-in icon factory to load your drawable val markerIcon = symbolManager.iconFactory.fromResource(R.drawable.ic_marker) // Build your marker (this replaces MarkerOptions) val markerSymbol = SymbolOptions() .withLatLng(LatLng(lat, lng)) .withIcon(markerIcon) .withIconSize(1.0f) // Adjust this number to make the marker bigger/smaller // Add the marker to the map symbolManager.create(markerSymbol)
Replacing mapboxMap.clear()
The old clear() method was deprecated because it wipes everything on the map (including layers you might want to keep). Instead, use the SymbolManager to only remove the markers you added:
// Delete all markers managed by this SymbolManager symbolManager.deleteAll()
If you need to clear other map elements (like custom layers), you’ll have to target them individually, but for markers from the Annotation Plugin, deleteAll() is the perfect replacement.
Quick Tips
- Make sure you initialize the SymbolManager after the MapboxMap is fully loaded (don’t try to set it up before
onMapReadyfires). - If you need to manage different groups of markers, you can create separate SymbolManager instances or use data source filters to organize them.
内容的提问来源于stack exchange,提问作者Radesh

