[SOLVED] How to add multiple GeoJsonLayer runtime and get click event in android


This Content is from Stack Overflow. Question asked by tGitm

I am trying to add multiple GeoJsonLayer. When user click on 1 polygon I would like to display data of clicked polygon. Those are some of my polygons:

![enter image description here

This is my function in which I get data from API and draw geojson layers on google maps.

override fun onMapReady(googleMap: GoogleMap) {
        mMap = googleMap
        var geojson = ArrayList<GeojsonResponse>()

        val userGerkId: String? = SharedPrefManager.getInstance(applicationContext).user.gerkMID

        RetrofitClientLands.instance.getLand(userGerkId).enqueue(object : Callback<GeojsonResponse> {
            override fun onResponse(
                call: Call<GeojsonResponse>,
                response: Response<GeojsonResponse>
            ) {
                if (response.code() == 200) {
                    val body = response.body()

                    if (body != null) {
                        for (i in 0 until body.lands.size) {
                            val geo = body.lands[i]

                            val geos = geo.get("geometry")
                            val properties = geo.get("properties")

                            //Log.i("Properties", properties.toString())
                            val geometryJson: JSONObject = JSONObject(geos.toString())
                            val geoJsonData: JSONObject = geometryJson

                            val layer = GeoJsonLayer(mMap, geoJsonData)
                            val style: GeoJsonPolygonStyle = layer.defaultPolygonStyle
                            style.fillColor = resources.getColor(R.color.darkGray)
                            style.strokeColor = resources.getColor(R.color.darkerGray)
                            style.strokeWidth = 2f

                                GeoJsonOnFeatureClickListener { feature: Feature ->
                                        "GeoJSON polygon clicked: $properties",

                    } else {
                        Log.i("Map-error", response.errorBody().toString())

            override fun onFailure(call: Call<GeojsonResponse>, t: Throwable) {
                Log.i("Map response", t.message.toString())
                    "Prišlo je do napake, na novo zaženite aplikacijo",

        // adding marker
        mMap.moveCamera(CameraUpdateFactory.newLatLng(LatLng(45.92757404830929, 15.595209429220395)))
        mMap.uiSettings.isZoomControlsEnabled = true
        mMap.animateCamera( CameraUpdateFactory.zoomTo( 12.5f ) );


I tried to set style.isClickable = false and then add code below, but every time i clicked on layer, it returns the same data (because whole map is clickable ig).

mMap.setOnMapClickListener {
    Log.i("Map_clicked", "polygon: $properties")

So is there any other way of doing this? This thread has the same problem described.
How to add multiple GeoJsonLayer runtime and get click event in android


this is not the best solution but if you disable the click of the layer then you will get an event in setOnMapClickListener

so I added this to get click event style.isClickable = false this will pervert the layer click event

val style: GeoJsonPolygonStyle = layer.defaultPolygonStyle
        style.fillColor = R.color.map_highlight
        style.strokeColor = R.color.map_highlight_border
        style.strokeWidth = 1f
        style.isClickable = false // this will pervert setOnFeatureClickListener so  mMap.setOnMapClickListener work

you will receieve click event in this part

mMap.setOnMapClickListener {
            val geocoder = Geocoder(this, Locale.getDefault())
            val addresses: List<Address> = geocoder.getFromLocation(it.latitude, it.longitude, 1)
            if (addresses.isNotEmpty()) {
                val country: String? = addresses[0].getCountryName()
                Log.e(TAG, "country is ${country.toString()}")
                Toast.makeText(this, country.toString(), Toast.LENGTH_SHORT).show()

this is working for me please do correct me if I am doing some wrong

This Question was asked in StackOverflow by Chirag Patel and Answered by Chirag Patel It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?