Sijan Neupane
Sijan Neupane

Reputation: 116

Drawing Polygon from LatLng List on mapFragment Android

I was unable to draw a polygon with latlng list! I saved the latlang in an activity and passed it to another using sharedpref, on receiving the string and extracting the latlng it works fine, but I'm unable to plot polygon with the Arraylist of latlng. The format of the coordinates ArrayList is also correct. I don't know what I missed here!

    //CONVERSION
    val coordinates = ArrayList<LatLng>()

    var answer: String = points.toString()
    answer = answer.replace("lat/lng:", "")
    answer = answer.replace(")", "]")
    answer = answer.replace("(", "[")

    try {
        val jsonArray = JSONArray(answer)
        for (i in 0 until jsonArray.length()) {
            val latLong = jsonArray.getJSONArray(i)
            val lat = latLong.getDouble(0)
            val lon = latLong.getDouble(1)
            coordinates.add(LatLng(lat, lon))
        }
    } catch (e: JSONException) {
        e.printStackTrace()
    }
    val poly = PolygonOptions()
    for (latLng in coordinates) {
     
        poly.addAll(Collections.singleton(latLng))
        poly.add(latLng)
        gMap.addMarker(MarkerOptions().position(latLng))
    
    }


    val polygon: Polygon = gMap.addPolygon(poly)

    polygon.strokeColor = Color.BLUE
    polygon.fillColor = Color.BLUE

Printing arrayList coordinates gives:

[lat/lng: (-18.062271504071017,-1.4062657579779625), lat/lng: (-18.062271504071017,-1.4062657579779625), lat/lng: (-18.062271504071017,-1.4062657579779625)]

But I got no polygon drawn on the map!

Upvotes: 0

Views: 83

Answers (1)

Andrii Omelchenko
Andrii Omelchenko

Reputation: 13353

Seems your polygone has no size - all of three vertex has same coordinates (-18.062271504071017,-1.4062657579779625):

[lat/lng: (-18.062271504071017,-1.4062657579779625), lat/lng: (-18.062271504071017,-1.4062657579779625), lat/lng: (-18.062271504071017,-1.4062657579779625)]

Upvotes: 1

Related Questions