在 MapBox Android SDK 中为来自 geoJSON 的多边形着色

Colorize polygons shapes from geoJSON in MapBox Android SDK

我正在使用 geoJSON 在 Android MapBox 中显示形状。 在 geoJSON 中,我有很多多边形,每个多边形在“属性”JSON对象中都有一个值,这是一个例子:

{
"geometry": {
    "type": "Polygon",
    "coordinates": [
        [
            [
                2.3303745,
                39.841098
            ],
            [
                2.3303464,
                39.8410976
            ],
            [
                2.3303261,
                39.8411054
            ]
        ]
    ]
},
"type": "Feature",
"properties": {
    "value": 169
}
}

我想根据值用特定颜色填充多边形。

我目前所做的和得到的

GeoJsonSource geoJsonSource = new GeoJsonSource("geojson",stringbuilder.toString());
mapboxMap.addSource(geoJsonSource);
mapboxMap.addLayer(new FillLayer("geojson", "geojson"));

问题

我应该怎么做才能给形状上色?

  1. 在JSON中添加一个“填充”值到“poperties”对象? (还没有为我工作。)
  2. 手动解析 JSON 并在“PolygonOptions”对象中使用“fillColor”函数? (会增加很多工作,因为有很多多边形)

我想要的

在数据驱动的样式出现在下一个版本中之前,您需要为每种颜色创建单独的图层并将它们叠加在一起。与您的想法相反,这应该不会影响性能,只是需要多做一些工作:)

JSONArray features = json.getJSONArray("features");

//Get the value for each features and create geojsonsource
for (int i = 0; i < features.length(); i++) {
    JSONObject feature = features.getJSONObject(i);
    Double value=-1.0;

    if (feature != null) {
        JSONObject properties = feature.getJSONObject("properties");
        if (properties != null && properties.has("value")) {
            value = properties.getDouble("value");
        }
        GeoJsonSource geoJsonSource = new GeoJsonSource("geojson" + i, feature.toString());

        if (!values.contains(value))
            values.add(value);
        list_value_geojson.add(new AbstractMap.SimpleEntry<>(value, geoJsonSource));
    }
}

在我的 AsyncTask 的 postExecute 函数中:

for (int i=0; i < list_value_geojson.size(); i++){
    Map.Entry<Double, GeoJsonSource> entry = list_value_geojson.get(i);
    if (mapboxMap != null && entry != null) {
        mapboxMap.addSource(entry.getValue());
        FillLayer fillLayer = new FillLayer(entry.getValue().getId(), entry.getValue().getId());
        fillLayer.setProperties(PropertyFactory.fillColor(Color.parseColor(hashMap    Colors.get(entry.getKey()))));
    mapboxMap.addLayer(fillLayer);

    }
}