how to parsing data from json to draw polygon on google map Android

Viewed 73

i'am working with data to draw polygon on google map

i have json data like this

{
  "statistic": [
    {
      "subdistrict": "Cempaka",
      "total_crime": 41,
      ........,
      "polygon_coordinate": [
        {
          "lat": -6.213643,
          "long": 106.900712
        },
        {
          "lat": -6.213318,
          "long": 106.900812
        },
        ........
      ]
    },
    {
      "subdistrict": "Cakung",
      "total_crime": 23,
      ........,
      "polygon_coordinate": [
        {
          "lat": -6.213643,
          "long": 106.900712
        },
        {
          "lat": -6.213318,
          "long": 106.900812
        },
        ........
      ]
    }
  ]
}

i have done like this in my MapFragment.kt

private fun drawSubdistrict() {
        val jsonFileString = readJsonFile(requireContext(), "area_statistic.json")
        val data: AreaStatisticDataModel =
            Gson().fromJson(jsonFileString, AreaStatisticDataModel::class.java)

        val coordinates = ArrayList<List<CoordinatesItem>>()
        val points: MutableList<LatLng> = ArrayList()


        for (i in data.statistic?.indices!!) {
            coordinates.add(data.statistic[i]?.coordinates as List<CoordinatesItem>)
        }

        for (i in coordinates.indices) {
            val size = coordinates[i].size
            for (j in 0 until size) {
                val lat = coordinates[i][j].lat
                val long = coordinates[i][j].long
                val latLng = LatLng(lat!!, long!!)
                points.add(latLng)
            }
            mMap.addPolygon(PolygonOptions().addAll(points).fillColor(0x22FF0000))
        }
    }

but the result like this

enter image description here

the questions is, how to process the data so i can draw the polygon for each subdistrict. thank you

0 Answers
Related