我正在使用adding the marker to the map style as a new layer
的推荐方式添加标记
List<Feature> symbolLayerIconFeatureList .....
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
symbolLayerIconFeatureList = new ArrayList<>();
symbolLayerIconFeatureList.add(Feature.fromGeometry(
Point.fromLngLat(-57.225365, -33.213144)));
symbolLayerIconFeatureList.add(Feature.fromGeometry(
Point.fromLngLat(-54.14164, -33.981818)));
symbolLayerIconFeatureList.add(Feature.fromGeometry(
Point.fromLngLat(-56.990533, -30.583266)));
mapboxMap.setStyle(new Style.Builder().fromUri("mapbox://styles/mapbox/cjf4m44iw0uza2spb3q0a7s41")
// Add the SymbolLayer icon image to the map style
.withImage(ICON_ID, BitmapFactory.decodeResource(
MainActivity.this.getResources(), R.drawable.red_marker))
// Adding a GeoJson source for the SymbolLayer icons.
.withSource(new GeoJsonSource(SOURCE_ID,
FeatureCollection.fromFeatures(symbolLayerIconFeatureList)))
// Adding the actual SymbolLayer to the map style. An offset is added that the bottom of the red
// marker icon gets fixed to the coordinate, rather than the middle of the icon being fixed to
// the coordinate point. This is offset is not always needed and is dependent on the image
// that you use for the SymbolLayer icon.
.withLayer(new SymbolLayer(LAYER_ID, SOURCE_ID)
.withProperties(PropertyFactory.iconImage(ICON_ID),
iconAllowOverlap(true),
iconOffset(new Float[]{0f, -9f}))
), new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
// Map is set up and the style has loaded. Now you can add additional data or make other map adjustments.
}
});
}
我想在运行时随意添加或删除其中一个标记,我们假设这是在api调用之后进行的。 我已经尝试过了:
markerCoordinates.remove(Feature.fromGeometry(
Point.fromLngLat(-57.225365, -33.213144)));
mapView.invalidate();
但是它不起作用。 其他选项建议我删除该层,但没有用。 有什么建议吗?
答案 0 :(得分:1)
只需几个步骤即可完成
:GeoJsonSource source = mapboxMap.getSourceAs(SOURCE_ID);
symbolLayerIconFeatureList
。source
提供新功能列表source.setGeoJson(FeatureCollection.fromFeatures(symbolLayerIconFeatureList));
你很好。
答案 1 :(得分:0)
您可以通过sourceId删除标记。例如:-mapStyle.removeSource(sourceId)