Android Google Maps GeoJsonLayer OnFeatureClickListener, multiple layers

岁酱吖の 提交于 2019-12-10 21:08:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!