在 MapBox 上添加标记的最佳方式是什么?
What is the best way to add markers on MapBox?
众所周知,mapbox.addMarker()
已弃用!
我试图找出使用 Symbol Layer
添加标记的新方法
我正在使用这段代码,它对我有用!但是每次我想添加标记时它都会重新定义地图的样式
代码如下:
//Add Marker to the Map
public void addMarker(@NonNull LatLng point) {
Feature feature = Feature.fromGeometry(Point.fromLngLat(point.getLongitude(), point.getLatitude()));
mapboxMap
.setStyle(new Style.Builder()
.fromUri("mapbox://styles/mapbox/cjf4m44iw0uza2spb3q0a7s41")
.withImage(ICON_ID, marker)
.withSource(new GeoJsonSource(SOURCE_ID, feature))
.withLayer(new SymbolLayer(LAYER_ID, SOURCE_ID)
.withProperties(PropertyFactory.iconImage(ICON_ID),
iconAllowOverlap(true),
iconIgnorePlacement(true),
iconOffset(new Float[]{0f, -9f}))));
}
所以,我想知道在最新的 MapBox SDK 8.5.0
上添加标记是否有正确或更好的方法
谢谢!
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull final Style style) {
AddSanciangkoStreet(style);
SanciangkoStreetMarker();
}
}
private void AddSanciangkoStreet(@NonNull Style style) {
style.addImage("sanciangko-street",
BitmapUtils.getBitmapFromDrawable(getResources().getDrawable(R.drawable.floodicon)));
style.addSource(new GeoJsonSource("sanciangkoFlood1-source-id"));
style.addLayer(new SymbolLayer("sanciangkoFlood1-layer-id", "sanciangkoFlood1-source-id").withProperties(
iconImage("sanciangko-street"),
iconIgnorePlacement(true),
iconAllowOverlap(true),
iconSize(1f)
));
}
private void SanciangkoStreetMarker() {
if (map.getStyle() != null) {
GeoJsonSource sanciangkoFlood1 = map.getStyle().getSourceAs("sanciangkoFlood1-source-id");
if (sanciangkoFlood1 != null) {
sanciangkoFlood1.setGeoJson(FeatureCollection.fromFeature(
Feature.fromGeometry(Point.fromLngLat(123.895801, 10.297237))
));
}
}
}
众所周知,mapbox.addMarker()
已弃用!
我试图找出使用 Symbol Layer
我正在使用这段代码,它对我有用!但是每次我想添加标记时它都会重新定义地图的样式
代码如下:
//Add Marker to the Map
public void addMarker(@NonNull LatLng point) {
Feature feature = Feature.fromGeometry(Point.fromLngLat(point.getLongitude(), point.getLatitude()));
mapboxMap
.setStyle(new Style.Builder()
.fromUri("mapbox://styles/mapbox/cjf4m44iw0uza2spb3q0a7s41")
.withImage(ICON_ID, marker)
.withSource(new GeoJsonSource(SOURCE_ID, feature))
.withLayer(new SymbolLayer(LAYER_ID, SOURCE_ID)
.withProperties(PropertyFactory.iconImage(ICON_ID),
iconAllowOverlap(true),
iconIgnorePlacement(true),
iconOffset(new Float[]{0f, -9f}))));
}
所以,我想知道在最新的 MapBox SDK 8.5.0
上添加标记是否有正确或更好的方法谢谢!
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull final Style style) {
AddSanciangkoStreet(style);
SanciangkoStreetMarker();
}
}
private void AddSanciangkoStreet(@NonNull Style style) {
style.addImage("sanciangko-street",
BitmapUtils.getBitmapFromDrawable(getResources().getDrawable(R.drawable.floodicon)));
style.addSource(new GeoJsonSource("sanciangkoFlood1-source-id"));
style.addLayer(new SymbolLayer("sanciangkoFlood1-layer-id", "sanciangkoFlood1-source-id").withProperties(
iconImage("sanciangko-street"),
iconIgnorePlacement(true),
iconAllowOverlap(true),
iconSize(1f)
));
}
private void SanciangkoStreetMarker() {
if (map.getStyle() != null) {
GeoJsonSource sanciangkoFlood1 = map.getStyle().getSourceAs("sanciangkoFlood1-source-id");
if (sanciangkoFlood1 != null) {
sanciangkoFlood1.setGeoJson(FeatureCollection.fromFeature(
Feature.fromGeometry(Point.fromLngLat(123.895801, 10.297237))
));
}
}
}