如何使用 MAPBOX android SDK add/remove 标记
How to add/remove markers with MAPBOX android SDK
我正在使用 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();
但是不行。
其他选项建议我删除该层,但它没有用。
有什么建议吗?
您可以通过 sourceId 删除标记。例如:- mapStyle.removeSource(sourceId)
只需几个步骤即可完成:
- 使用
GeoJsonSource source = mapboxMap.getSourceAs(SOURCE_ID);
获取图层源
- 通过添加或删除您想要的任何功能来更新您的功能列表
symbolLayerIconFeatureList
。
- 给你的
source
新功能列表 source.setGeoJson(FeatureCollection.fromFeatures(symbolLayerIconFeatureList));
你很高兴。
我正在使用 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();
但是不行。 其他选项建议我删除该层,但它没有用。 有什么建议吗?
您可以通过 sourceId 删除标记。例如:- mapStyle.removeSource(sourceId)
只需几个步骤即可完成:
- 使用
GeoJsonSource source = mapboxMap.getSourceAs(SOURCE_ID);
获取图层源
- 通过添加或删除您想要的任何功能来更新您的功能列表
symbolLayerIconFeatureList
。 - 给你的
source
新功能列表source.setGeoJson(FeatureCollection.fromFeatures(symbolLayerIconFeatureList));
你很高兴。