22
33import android .annotation .SuppressLint ;
44import android .content .Context ;
5+ import android .graphics .Bitmap ;
56import android .graphics .PointF ;
67import android .location .Location ;
78import android .os .Bundle ;
1314
1415import com .mapbox .api .directions .v5 .models .DirectionsRoute ;
1516import com .mapbox .geojson .Point ;
16- import com .mapbox .mapboxsdk .annotations .Icon ;
17- import com .mapbox .mapboxsdk .annotations .Marker ;
18- import com .mapbox .mapboxsdk .annotations .MarkerOptions ;
1917import com .mapbox .mapboxsdk .geometry .LatLng ;
2018import com .mapbox .mapboxsdk .location .LocationComponent ;
2119import com .mapbox .mapboxsdk .location .LocationComponentOptions ;
2422import com .mapbox .mapboxsdk .maps .MapView ;
2523import com .mapbox .mapboxsdk .maps .MapboxMap ;
2624import com .mapbox .mapboxsdk .maps .OnMapReadyCallback ;
25+ import com .mapbox .mapboxsdk .plugins .annotation .SymbolManager ;
2726import com .mapbox .mapboxsdk .style .sources .Source ;
2827import com .mapbox .mapboxsdk .style .sources .VectorSource ;
2928import com .mapbox .services .android .navigation .ui .v5 .NavigationSnapshotReadyCallback ;
3433import com .mapbox .services .android .navigation .ui .v5 .route .OnRouteSelectionChangeListener ;
3534import com .mapbox .services .android .navigation .v5 .navigation .MapboxNavigation ;
3635
37- import java .util .ArrayList ;
3836import java .util .List ;
3937
38+ import static com .mapbox .services .android .navigation .ui .v5 .map .MapSymbolManager .MAPBOX_NAVIGATION_MARKER_NAME ;
4039import static com .mapbox .services .android .navigation .v5 .navigation .NavigationConstants .NAVIGATION_MINIMUM_MAP_ZOOM ;
4140
4241/**
@@ -59,10 +58,10 @@ public class NavigationMapboxMap {
5958 private static final String TRAFFIC_LAYER_ID = "traffic" ;
6059 private static final int [] ZERO_MAP_PADDING = {0 , 0 , 0 , 0 };
6160 private static final double NAVIGATION_MAXIMUM_MAP_ZOOM = 18d ;
62- private final List <Marker > mapMarkers = new ArrayList <>();
6361 private MapboxMap mapboxMap ;
6462 private LocationComponent locationComponent ;
6563 private MapPaddingAdjustor mapPaddingAdjustor ;
64+ private MapSymbolManager mapSymbolManager ;
6665 private MapLayerInteractor layerInteractor ;
6766 private MapWayName mapWayName ;
6867 private NavigationMapRoute mapRoute ;
@@ -80,6 +79,7 @@ public NavigationMapboxMap(@NonNull MapView mapView, @NonNull MapboxMap mapboxMa
8079 this .mapboxMap = mapboxMap ;
8180 initializeLocationComponent (mapView , mapboxMap );
8281 initializeMapPaddingAdjustor (mapView , mapboxMap );
82+ initializeMapSymbolManager (mapView , mapboxMap );
8383 initializeMapLayerInteractor (mapboxMap );
8484 initializeWayname (mapboxMap , mapPaddingAdjustor );
8585 initializeRoute (mapView , mapboxMap );
@@ -128,8 +128,7 @@ public NavigationMapboxMap(@NonNull MapView mapView, @NonNull MapboxMap mapboxMa
128128 * @param position the point at which the marker will be placed
129129 */
130130 public void addMarker (Context context , Point position ) {
131- Marker marker = createMarkerFromIcon (context , position );
132- mapMarkers .add (marker );
131+ mapSymbolManager .addMarkerFor (position );
133132 }
134133
135134 /**
@@ -139,7 +138,7 @@ public void addMarker(Context context, Point position) {
139138 * if no markers have been added.
140139 */
141140 public void clearMarkers () {
142- removeAllMarkers ();
141+ mapSymbolManager . removeAllMarkerSymbols ();
143142 }
144143
145144 /**
@@ -567,6 +566,13 @@ private void initializeMapPaddingAdjustor(MapView mapView, MapboxMap mapboxMap)
567566 mapPaddingAdjustor = new MapPaddingAdjustor (mapView , mapboxMap );
568567 }
569568
569+ private void initializeMapSymbolManager (MapView mapView , MapboxMap mapboxMap ) {
570+ Bitmap markerBitmap = ThemeSwitcher .retrieveThemeMapMarker (mapView .getContext ());
571+ mapboxMap .getStyle ().addImage (MAPBOX_NAVIGATION_MARKER_NAME , markerBitmap );
572+ SymbolManager symbolManager = new SymbolManager (mapView , mapboxMap , mapboxMap .getStyle ());
573+ mapSymbolManager = new MapSymbolManager (symbolManager );
574+ }
575+
570576 private void initializeMapLayerInteractor (MapboxMap mapboxMap ) {
571577 layerInteractor = new MapLayerInteractor (mapboxMap );
572578 }
@@ -633,22 +639,6 @@ private void removeFpsListenersFromCamera() {
633639 mapCamera .removeOnTrackingModeChangedListener (mapFpsDelegate );
634640 }
635641
636- @ NonNull
637- private Marker createMarkerFromIcon (Context context , Point position ) {
638- LatLng markerPosition = new LatLng (position .latitude (),
639- position .longitude ());
640- Icon markerIcon = ThemeSwitcher .retrieveThemeMapMarker (context );
641- return mapboxMap .addMarker (new MarkerOptions ()
642- .position (markerPosition )
643- .icon (markerIcon ));
644- }
645-
646- private void removeAllMarkers () {
647- for (Marker marker : mapMarkers ) {
648- mapboxMap .removeMarker (marker );
649- }
650- }
651-
652642 private void updateMapWaynameWithLocation (Location location ) {
653643 LatLng latLng = new LatLng (location );
654644 PointF mapPoint = mapboxMap .getProjection ().toScreenLocation (latLng );
0 commit comments