简介
在之前的教程中,您学习了如何将 KML 和 GeoJSON 地图项添加到地图,以及如何添加标记的聚类。但是,如果您想在同一个地图中添加多个这样的图层,然后单独获取每个图层的点击事件,该怎么操作呢?
添加多个聚类、KML 和 GeoJSON 图层
该库包含 Manager
对象,可帮助管理多种类型的图层的点击事件。因此,在设置图层之前,您需要先将这些图层实例化并传入您的 GoogleMap
:
Kotlin
val markerManager = MarkerManager(map) val groundOverlayManager = GroundOverlayManager(map!!) val polygonManager = PolygonManager(map) val polylineManager = PolylineManager(map)
Java
MarkerManager markerManager = new MarkerManager(map); GroundOverlayManager groundOverlayManager = new GroundOverlayManager(map); PolygonManager polygonManager = new PolygonManager(map); PolylineManager polylineManager = new PolylineManager(map);
接下来,在设置其他图层时,您可以将这些管理器类传递到相应图层的构造函数中:
Kotlin
val clusterManager = ClusterManager<MyItem>(context, map, markerManager) val geoJsonLineLayer = GeoJsonLayer( map, R.raw.geojson_file, context, markerManager, polygonManager, polylineManager, groundOverlayManager ) val kmlPolylineLayer = KmlLayer( map, R.raw.kml_file, context, markerManager, polygonManager, polylineManager, groundOverlayManager, null )
Java
ClusterManager<MyItem> clusterManager = new ClusterManager<>(context, map, markerManager); GeoJsonLayer geoJsonLineLayer = new GeoJsonLayer(map, R.raw.geojson_file, context, markerManager, polygonManager, polylineManager, groundOverlayManager); KmlLayer kmlPolylineLayer = new KmlLayer(map, R.raw.kml_file, context, markerManager, polygonManager, polylineManager, groundOverlayManager, null);
添加您自己的地图项
如果除了这些图层,您还想添加自己的标记、地面叠加层、多段线或多边形,请创建自己的 Collection
,然后使用 Managers
添加相应地图项,而不是直接将其添加到 GoogleMap
对象中。添加新标记的示例如下:
Kotlin
val markerCollection = markerManager.newCollection() markerCollection.addMarker( MarkerOptions() .position(LatLng(51.150000, -0.150032)) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)) .title("Unclustered marker") )
Java
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"));
处理点击事件
对于聚类、KML 和 GeoJSON,只要在您正在设置的图层的构造函数中传入 Manager
类,点击监听器即可正常运行。以下示例展示了如何为 KML 图层设置点击监听器:
Kotlin
kmlPolylineLayer.addLayerToMap() kmlPolylineLayer.setOnFeatureClickListener { feature: Feature -> Toast.makeText(context, "KML polyline clicked: ${feature.getProperty("name")}", Toast.LENGTH_SHORT ).show() }
Java
kmlPolylineLayer.addLayerToMap(); kmlPolylineLayer.setOnFeatureClickListener(feature -> Toast.makeText(context, "KML polyline clicked: " + feature.getProperty("name"), Toast.LENGTH_SHORT).show());
当您添加自己的标记、地面叠加层、多段线或多边形时,请务必确保向这些 Collection
对象添加点击监听器。以下示例展示了如何在 markerCollection
上设置标记点击监听器:
Kotlin
markerCollection.setOnMarkerClickListener { marker: Marker -> Toast.makeText( context, "Marker clicked: ${marker.title}", Toast.LENGTH_SHORT ).show() false }
Java
markerCollection.setOnMarkerClickListener(marker -> { Toast.makeText(context, "Marker clicked: " + marker.getTitle(), Toast.LENGTH_SHORT).show(); return false; });
观摩演示版应用
如需查看添加多个图层的示例,请参见实用程序库附带的演示版应用中的 MultiLayerDemoActivity
。设置指南介绍了如何运行演示版应用。