【问题标题】:How do I get the color value from the List<Feature> list?如何从 List<Feature> 列表中获取颜色值?
【发布时间】:2020-03-18 11:24:01
【问题描述】:

我在 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);

        });

答案将非常有用。谢谢

【问题讨论】:

    标签: java android dictionary mapbox


    【解决方案1】:

    您可以执行以下操作,而不是使用 JsonObjects

    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/ 中的操作。

    1. 在设置 SymbolLayer 时不要使用 .withImage()

    2. 您的 SymbolLayer 将是 .iconImage(get("marker-color")),

    【讨论】:

    • 太棒了!很高兴听到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 2019-11-06
    • 1970-01-01
    • 2011-10-10
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多