Hi, I have an issue where the GeoJsonLayer's onFeatureClickListener doesn't trigger.
I expected it to work after adding: layer.setOnFeatureClickListener ...
The layer registers the click event, but the debug log message doesn't show, which makes me believe that the onFeatureClickListener didn't trigger.
My Google Maps key is locked to my app, so I don't think I can reproduce it in the demo app?
However, what I do is:
private GeoJsonLayer layer=null;layer=GeoJsonLayer(map, R.raw.my_layer, this);GeoJsonPolygonStyle layer_style=layer.defaultPolygonStyle;
layer_style.isClickable=true;
layer_style.strokeWidth=3f;
layer_style.strokeColor=my_color;
layer.addLayerToMap();layer.setOnFeatureClickListener(new GeoJsonLayer.GeoJsonOnFeatureClickListener(){
@Override public void onFeatureClick(Feature feature) {
Log.d("MapActivity","layer clicked!")
}
})
The result is that when I press "layer", it overrides the click event for the map, but the onFeatureClick event doesn't trigger.
I'm sorry if some of the code is wrong, but I'm actually using it with Kotlin.
@Erlend2 Thanks for reaching out! When instantiating the GeoJsonLayer, try passing in the manager objects - you can see a demo in the MultiLayerDemoActivity:
MarkerManager markerManager = new MarkerManager(getMap());
GroundOverlayManager groundOverlayManager = new GroundOverlayManager(getMap());
PolygonManager polygonManager = new PolygonManager(getMap());
PolylineManager polylineManager = new PolylineManager(getMap());
...
// GeoJSON polygon
GeoJsonLayer geoJsonPolygonLayer = new GeoJsonLayer(getMap(), R.raw.south_london_square_geojson, this, markerManager, polygonManager, polylineManager, groundOverlayManager);
Please give that a shot and see if it fixes the issue.
@barbeau Thanks for the solution, it works now! The logcat debug message appeared. Would you happen to know why the OnFeatureClickListener works after passing the manager objects?
@Erlend2 That's great! In v1.0, to support multiple layers on the map at once (KML, GeoJSON, clusters, etc.), a centralized object (the manager) is used to manage and dispatch events. This is required because the underlying Maps API only supports a single listener per event (e.g., single click listener). More details in the Migration Guide here:
https://github.com/googlemaps/android-maps-utils#migration-guide
I'm going to close out the issue, but please feel free to comment or re-open if the problem isn't resolved.
Hello again, @barbeau, I could open another issue if preferable, but I have a potentially related issue now with the OnMarkerClickListener for Markers.
I have added the interface to the activity, overridden the function onMarkerClick, and added map.setOnMarkerClickListener(this) in the onMapReady callback. The Markers are fully clickable, but the clicks aren't registered in onMarkerClick...
@Erlend2 It's a similar issue to the above - instead of adding the click listener directly on the GoogleMap, try using a MarkerManager and adding the click listener directly to that. See an example in the MultiLayerDemoActivity:
// Unclustered marker - instead of adding to the map directly, use the MarkerManager
MarkerManager.Collection markerCollection = markerManager.newCollection();
markerCollection.addMarker(new MarkerOptions()
.position(new LatLng(51.150000, -0.150032))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("Unclustered marker"));
markerCollection.setOnMarkerClickListener(marker -> {
Toast.makeText(MultiLayerDemoActivity.this,
"Marker clicked: " + marker.getTitle(),
Toast.LENGTH_SHORT).show();
return false;
});
Please give that a try!
@barbeau Thank you, it works perfectly now!
Just one thing though, any obvious way to set tag and id to the markers added to the Marker collection? Now an id is automatically set as "mx", e.g. "m0", "m1", "m2", and so on, ordered according to when they were added.
@Erlend2 Hmm, good question. The Marker ID is set automatically by the underlying Maps SDK and is unique among all markers on the map, so to my knowledge this library (and you) can't change that.
I believe the Marker.setTag() method is really what you want. IIRC the library doesn't directly expose this, but you could just get a returned Marker from the manager addMarker() method and then add your tag - something like:
Marker newMarker = markerCollection.addMarker(new MarkerOptions()
.position(new LatLng(51.150000, -0.150032))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("Unclustered marker"));
newMarker.setTag("MyID");
Let me know if that works!
@barbeau It worked (again), thank you! I really appreciate all your help.
@Erlend2 You're welcome, glad it worked!