Android Google 地图 GeoJsonLayer OnFeatureClickListener,多层
Android Google Maps GeoJsonLayer OnFeatureClickListener, multiple layers
请查看我的代码以从 geojson 字符串创建图层并将图层添加到地图:
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;
}
接下来向地图添加 2 个图层:
String json = /*first geojson string here*/
String json2 = /*another geojson string here*/
createLayerFromGeojson(json);
createLayerFromGeojson(json2);
问题:当我点击标记或 pologon 时,总是显示取自第二个 json (json2) 的描述,即使我点击第一个 json 创建的对象也是如此, 第一层.
怎么了?有什么想法吗?
如果您查看方法 setOnFeatureClickListener
的文档,它说:
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.
对我来说,我们不能拥有包含来自不同 GeoJson 的信息的多层,这似乎很愚蠢。它需要是 MultiPolygon、MultiLineString 或 MultiPoint。
请查看我的代码以从 geojson 字符串创建图层并将图层添加到地图:
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;
}
接下来向地图添加 2 个图层:
String json = /*first geojson string here*/
String json2 = /*another geojson string here*/
createLayerFromGeojson(json);
createLayerFromGeojson(json2);
问题:当我点击标记或 pologon 时,总是显示取自第二个 json (json2) 的描述,即使我点击第一个 json 创建的对象也是如此, 第一层.
怎么了?有什么想法吗?
如果您查看方法 setOnFeatureClickListener
的文档,它说:
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.
对我来说,我们不能拥有包含来自不同 GeoJson 的信息的多层,这似乎很愚蠢。它需要是 MultiPolygon、MultiLineString 或 MultiPoint。