0
votes

I created GeoJson with geojson.io:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "stroke": "#555555",
        "stroke-width": 1,
        "stroke-opacity": 1,
        "fill": "#cc1414",
        "fill-opacity": 0.5
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              38.91359567642212,
              47.25578268604025
            ],
            [
              38.91415894031525,
              47.25578268604025
            ],
            [
              38.91415894031525,
              47.25588827416263
            ],
            [
              38.91359567642212,
              47.25588827416263
            ],
            [
              38.91359567642212,
              47.25578268604025
            ]
          ]
        ]
      }
    }
  ]
}

There is fill color in properties:

"properties": {
            "stroke": "#555555",
            "stroke-width": 1,
            "stroke-opacity": 1,
            "fill": "#cc1414",
            "fill-opacity": 0.5
          }

Then I convert it from String to JSONObject like this: JSONObject(geoJsonString), and this is how I apply layer to the map:

fun drawPolygons(jsonObj: JSONObject) {
        map?.let { map ->
            val layer = GeoJsonLayer(map, jsonObj)
            layer.addLayerToMap()
        }
    }

But Google map ignoring all properties at all. No fill color, no stroke width. I need to apply it all from geojson string, without a code. How to do it?

1

1 Answers

2
votes

Properties in the GeoJSON are just a collection of key/value pairs. You shouldn't expect them to be rendered as features styles automatically. This is your task to read properties of the features and apply them as styles.

You can loop through features get value of the properties and apply corresponding style. Have a look at the code snapshot that shows how to set several polygon styles

fun drawPolygons(jsonObj: JSONObject) {
    map?.let { map ->
        val layer = GeoJsonLayer(map, jsonObj)
        for (feature in layer.features) {
            val polygonStyle = GeoJsonPolygonStyle()
            if (feature.hasProperty("stroke")) {
                polygonStyle.strokeColor = feature.getProperty("stroke")
            }
            if (feature.hasProperty("stroke-width")) {
                polygonStyle.strokeWidth = feature.getProperty("stroke-width")
            }
            if (feature.hasProperty("fill")) {
                polygonStyle.fillColor = feature.getProperty("fill")
            }
            feature.polygonStyle = polygonStyle
        }
        layer.addLayerToMap()
    }
}

For further details about Android Maps Utils library and its classes you can refer to JavaDoc located at

https://www.javadoc.io/doc/com.google.maps.android/android-maps-utils/latest/com/google/maps/android/data/geojson/GeoJsonFeature.html

Enjoy!