簡介
我們在先前的教學課程中,解說了如何在地圖上新增 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);
接著,在設定過程中,您可以將這些 Manager 類別傳遞至其他圖層的建構函式:
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
。設定指南會示範如何執行試用版應用程式。