Rotate and change position for markers in latest MapBox SDK 6.7

前端 未结 1 1167
逝去的感伤
逝去的感伤 2021-01-14 02:29

Mapbox Android SDK: 6.7.0

The requirement in the application we are developing is that we have to add multiple markers in different LatLng positions

1条回答
  •  天涯浪人
    2021-01-14 03:11

    This can be achieved by symbol layer in the latest SDK 6.7.0.

    To add a marker:

           Bitmap compassNeedleSymbolLayerIcon = BitmapFactory.decodeResource(
                    getResources(), R.drawable.compass_needle);
            mapboxMap.addImage(AIRCRAFT_MARKER_ICON_ID, compassNeedleSymbolLayerIcon);
    
           GeoJsonSource geoJsonSource = new GeoJsonSource(GEOJSON_SOURCE_ID, Feature.fromGeometry(
                    Point.fromLngLat(longitude, latitude)));
            mapboxMap.addSource(geoJsonSource);
    
            SymbolLayer Layer = new SymbolLayer(AIRCRAFT_LAYER_ID, GEOJSON_SOURCE_ID)
                    .withProperties(
                            PropertyFactory.iconImage(AIRCRAFT_MARKER_ICON_ID),
                            PropertyFactory.iconRotate((float) headDirection),
                            PropertyFactory.iconIgnorePlacement(true),
                            PropertyFactory.iconAllowOverlap(true)
                    );
            mapboxMap.addLayer(layer);
    

    To rotate or changing the position of the marker:

    GeoJsonSource source = mapboxMap.getSourceAs(GEOJSON_SOURCE_ID);
                if (source != null) {
                    source.setGeoJson(Feature.
                            fromGeometry(Point.fromLngLat(longitude, latitude)));
                    layer.setProperties(
                            PropertyFactory.iconRotate((float) headDirection)
                    );
                }
    

    This above code may not work sometimes when you add the marker in the onMapReady() callback. Because onMapReady() is called before all styles are loaded. Hence add the marker in addOnDidFinishLoadingStyleListener() callback.

    mapView.addOnDidFinishLoadingStyleListener(new MapView.OnDidFinishLoadingStyleListener() {
            @Override
            public void onDidFinishLoadingStyle() {
                //add marker here
            }
        });
    

    0 讨论(0)
提交回复
热议问题