如何从 List<Feature> 列表中获取颜色值?
How do I get the color value from the List<Feature> list?
我在 json 中指定了 object2.addProperty("marker-colors", "#ffbf00");
的颜色,然后检索该值。 withImage(ICON_ID, createBitMap(get("marker-color")))
我将在 createBitmap(...) 中使用颜色值
这是我的代码
List<Feature> features = new ArrayList<>();
JsonObject object1 = new JsonObject();
object1.addProperty("title", "TEST1");
object1.addProperty("marker-color", "#c70024");
features.add(Feature.fromGeometry(
Point.fromLngLat(106.535033, -6.323488), object1));
JsonObject object2 = new JsonObject();
object2.addProperty("title", "TEST2");
object2.addProperty("marker-color", "#ffbf00");
features.add(Feature.fromGeometry(
Point.fromLngLat(106.897423, -6.277078), object2));
JsonObject object3 = new JsonObject();
object3.addProperty("title", "TEST3");
object3.addProperty("marker-color", "#00c753");
features.add(Feature.fromGeometry(
Point.fromLngLat(106.797217, -6.171958), object3));
然后
mapboxMap.setStyle(new Style.Builder().fromUri(CPN_STYLE)
.withSource(new GeoJsonSource(SOURCE_ID,
FeatureCollection.fromFeatures(features)))
.withImage(ICON_ID, createBitMap(get("marker-color")))
.withLayer(new SymbolLayer(LAYER_ID, SOURCE_ID)
.withProperties(
textField(get("title")),
iconImage(ICON_ID),
textSize(14f),
iconAllowOverlap(true),
iconIgnorePlacement(true),
textColor(get("marker-color")),
textJustify(TEXT_JUSTIFY_AUTO),
iconOffset(new Float[]{0f, -9f}))
), style -> {
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(
new CameraPosition.Builder()
.target(new LatLng(-6.323488, 106.535033))
.zoom(7)
.build()), 1000);
});
答案将非常有用。谢谢
您可以执行以下操作而不是使用 JsonObject
s
Feature singleFeature = Feature.fromGeometry(Point.fromLngLat(LONG,LAT));
singleFeature.addStringProperty("title", "TEST1");
singleFeature.addStringProperty("marker-color", "#c70024");
features.add(singleFeature);
createBitmap()
方法在幕后是什么样子的?
您可能想要使用 Maps SDK 的 MapView.OnStyleImageMissingListener
并密切关注 https://docs.mapbox.com/android/maps/examples/missing-icon/ 中的内容。
在设置 SymbolLayer 时不要使用 .withImage()
。
您的 SymbolLayer 将 .iconImage(get("marker-color")),
。
我在 json 中指定了 object2.addProperty("marker-colors", "#ffbf00");
的颜色,然后检索该值。 withImage(ICON_ID, createBitMap(get("marker-color")))
我将在 createBitmap(...) 中使用颜色值
这是我的代码
List<Feature> features = new ArrayList<>();
JsonObject object1 = new JsonObject();
object1.addProperty("title", "TEST1");
object1.addProperty("marker-color", "#c70024");
features.add(Feature.fromGeometry(
Point.fromLngLat(106.535033, -6.323488), object1));
JsonObject object2 = new JsonObject();
object2.addProperty("title", "TEST2");
object2.addProperty("marker-color", "#ffbf00");
features.add(Feature.fromGeometry(
Point.fromLngLat(106.897423, -6.277078), object2));
JsonObject object3 = new JsonObject();
object3.addProperty("title", "TEST3");
object3.addProperty("marker-color", "#00c753");
features.add(Feature.fromGeometry(
Point.fromLngLat(106.797217, -6.171958), object3));
然后
mapboxMap.setStyle(new Style.Builder().fromUri(CPN_STYLE)
.withSource(new GeoJsonSource(SOURCE_ID,
FeatureCollection.fromFeatures(features)))
.withImage(ICON_ID, createBitMap(get("marker-color")))
.withLayer(new SymbolLayer(LAYER_ID, SOURCE_ID)
.withProperties(
textField(get("title")),
iconImage(ICON_ID),
textSize(14f),
iconAllowOverlap(true),
iconIgnorePlacement(true),
textColor(get("marker-color")),
textJustify(TEXT_JUSTIFY_AUTO),
iconOffset(new Float[]{0f, -9f}))
), style -> {
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(
new CameraPosition.Builder()
.target(new LatLng(-6.323488, 106.535033))
.zoom(7)
.build()), 1000);
});
答案将非常有用。谢谢
您可以执行以下操作而不是使用 JsonObject
s
Feature singleFeature = Feature.fromGeometry(Point.fromLngLat(LONG,LAT));
singleFeature.addStringProperty("title", "TEST1");
singleFeature.addStringProperty("marker-color", "#c70024");
features.add(singleFeature);
createBitmap()
方法在幕后是什么样子的?
您可能想要使用 Maps SDK 的 MapView.OnStyleImageMissingListener
并密切关注 https://docs.mapbox.com/android/maps/examples/missing-icon/ 中的内容。
在设置 SymbolLayer 时不要使用
.withImage()
。您的 SymbolLayer 将
.iconImage(get("marker-color")),
。