问题
Please look at my code to create layer from geojson string and add layer to map:
private GeoJsonLayer createLayerFromGeojson(String json)
{
JSONObject ob = null;
try
{
ob = new JSONObject(json);
}
catch (JSONException e)
{
e.printStackTrace();
}
GeoJsonLayer layer = new GeoJsonLayer(googleMap, ob);
layer.addLayerToMap();
layer.setOnFeatureClickListener(feature -> Utils.showMessage(getActivity(), "Clicked", feature.getProperty("description").toString()));
return layer;
}
Next add 2 layers to map:
String json = /*first geojson string here*/
String json2 = /*another geojson string here*/
createLayerFromGeojson(json);
createLayerFromGeojson(json2);
Problem: When I click on marker or pologon, always description taken from second json (json2) is displayed, even if I click on object created from first json, on first layer.
What's wrong? Any ideas?
回答1:
If you check the documentation for the method setOnFeatureClickListener
it says:
Sets a single click listener for the entire GoogleMap object, that will be called with the corresponding Feature object when an object on the map (Polygon, Marker, Polyline) is clicked.
To me, it seems silly that we cannot have multiple layers with information from different GeoJson. It needs to be a MultiPolygon, MultiLineString or MultiPoint.
Reference: https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/data/Layer.java#L89
来源:https://stackoverflow.com/questions/44017057/android-google-maps-geojsonlayer-onfeatureclicklistener-multiple-layers