Selecting features by onclick in Google Maps Android API

1.4k Views Asked by At

I have developing a map app by using Google Maps Android API. I used Google Maps Android API Utility Library for adding a GeoJSON layer (in polygon geometry).

String gj = loadJSONfromAssets();
GeoJsonLayer layer = new GeoJsonLayer(mMap, gj);

And also added a WMS layer as TileOverlay. I want map objects selectable. For example users can click on map objects (GeoJSON layer) and get their attributes. About this case I just found that only objects like Point, Polyline, Polygon can have click events. My question is: how can i set this event for all objects in a layer (GeoJSON layer).

2

There are 2 best solutions below

1
On

I found the example provided at https://github.com/googlemaps/android-maps-utils/blob/master/demo/src/com/google/maps/android/utils/demo/GeoJsonDemoActivity.java had a feature on click listener

// Demonstrate receiving features via GeoJsonLayer clicks.
    layer.setOnFeatureClickListener(new GeoJsonLayer.GeoJsonOnFeatureClickListener() {
        @Override
        public void onFeatureClick(GeoJsonFeature feature) {
            Toast.makeText(GeoJsonDemoActivity.this,
                    "Feature clicked: " + feature.getProperty("title"),
                    Toast.LENGTH_SHORT).show();
        }
    });
0
On

Any updates on this topic? I got the same problem.

 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
                            layer.addLayerToMap()

                            layer.setOnFeatureClickListener {
                                Log.i("Properties", properties.toString())
                            }
    }