Rotate and change position for markers in latest MapBox SDK 6.7

混江龙づ霸主 提交于 2019-12-01 06:17:53

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
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!