Google 地图多图层实用程序
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
- 简介
- 添加多个聚类、KML 和 GeoJSON 图层
- 添加您自己的地图项
- 处理点击事件
- 查看演示版应用
简介
在之前的教程中,您学习了如何将 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
。设置指南介绍了如何运行演示版应用。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-08-13。
[null,null,["最后更新时间 (UTC):2024-08-13。"],[[["\u003cp\u003eThis guide explains how to add and manage multiple map layers like KML, GeoJSON, and Clusters on the same map using the Maps SDK for Android Utility Library.\u003c/p\u003e\n"],["\u003cp\u003eIt covers using \u003ccode\u003eManager\u003c/code\u003e objects to handle click events for these layers and how to add your own features.\u003c/p\u003e\n"],["\u003cp\u003eClick listeners function as usual for KML, GeoJSON, and Clusters when the \u003ccode\u003eManager\u003c/code\u003e classes are passed to the layer's constructor.\u003c/p\u003e\n"],["\u003cp\u003eFor custom markers and overlays, use \u003ccode\u003eCollection\u003c/code\u003e objects and add click listeners to them for event handling.\u003c/p\u003e\n"],["\u003cp\u003eA demo app, \u003ccode\u003eMultiLayerDemoActivity\u003c/code\u003e, showcases the implementation of these concepts.\u003c/p\u003e\n"]]],[],null,["# Google Maps Multilayer Utility\n\nSelect platform: [Android](/maps/documentation/android-sdk/utility/multilayer \"View this page for the Android platform docs.\") [JavaScript](/maps/documentation/javascript/layers \"View this page for the JavaScript platform docs.\")\n\n1. [Introduction](#introduction)\n2. [Adding multiple cluster, KML, and GeoJSON layers](#add-multiple)\n3. [Adding your own features](#add-features)\n4. [Handling click events](#handle-click)\n5. [See the demo app](#demo-app)\n\nIntroduction\n------------\n\n\nIn previous tutorials, you've learned how to add features from\n[KML](/maps/documentation/android-sdk/utility/kml) and\n[GeoJSON](/maps/documentation/android-sdk/utility/geojson) to your map, as well as\n[clusters](/maps/documentation/android-sdk/utility/marker-clustering) of markers.\nBut what if you want to add several of these layers on the same map and get independent click\nevents for each?\n\nAdding multiple cluster, KML, and GeoJSON layers\n------------------------------------------------\n\n\nThe library includes `Manager`objects to help manage click events for multiple types\nof layers. So, before you set up your layers you'll first need to instantiate these and pass in\nyour `GoogleMap`: \n\n### Kotlin\n\n```kotlin\nval markerManager = MarkerManager(map)\nval groundOverlayManager = GroundOverlayManager(map!!)\nval polygonManager = PolygonManager(map)\nval polylineManager = PolylineManager(map)\n\n \n```\n\n### Java\n\n```java\nMarkerManager markerManager = new MarkerManager(map);\nGroundOverlayManager groundOverlayManager = new GroundOverlayManager(map);\nPolygonManager polygonManager = new PolygonManager(map);\nPolylineManager polylineManager = new PolylineManager(map);\n\n \n```\n\n\nNext, you can pass these manager classes into the constructors of the other layers when you\nset them up: \n\n### Kotlin\n\n```kotlin\nval clusterManager =\n ClusterManager\u003cMyItem\u003e(context, map, markerManager)\nval geoJsonLineLayer = GeoJsonLayer(\n map,\n R.raw.geojson_file,\n context,\n markerManager,\n polygonManager,\n polylineManager,\n groundOverlayManager\n)\nval kmlPolylineLayer = KmlLayer(\n map,\n R.raw.kml_file,\n context,\n markerManager,\n polygonManager,\n polylineManager,\n groundOverlayManager,\n null\n)\n\n \n```\n\n### Java\n\n```java\nClusterManager\u003cMyItem\u003e clusterManager = new ClusterManager\u003c\u003e(context, map, markerManager);\nGeoJsonLayer geoJsonLineLayer = new GeoJsonLayer(map, R.raw.geojson_file, context, markerManager, polygonManager, polylineManager, groundOverlayManager);\nKmlLayer kmlPolylineLayer = new KmlLayer(map, R.raw.kml_file, context, markerManager, polygonManager, polylineManager, groundOverlayManager, null);\n\n \n```\n\nAdding your own features\n------------------------\n\n\nIf you want to add your own markers, ground overlays, polylines, or polygons alongside these\nlayers, create your own `Collection` and then use the `Managers`\nto add the feature instead of directly adding them to the `GoogleMap` object.\n\nFor example, if you want to add a new marker: \n\n### Kotlin\n\n```kotlin\nval markerCollection =\n markerManager.newCollection()\nmarkerCollection.addMarker(\n MarkerOptions()\n .position(LatLng(51.150000, -0.150032))\n .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))\n .title(\"Unclustered marker\")\n)\n\n \n```\n\n### Java\n\n```java\nMarkerManager.Collection markerCollection = markerManager.newCollection();\nmarkerCollection.addMarker(new MarkerOptions()\n .position(new LatLng(51.150000, -0.150032))\n .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))\n .title(\"Unclustered marker\"));\n\n \n```\n\nHandling click events\n---------------------\n\n\nFor clusters, KML, and GeoJSON, click listeners work like normal - as long as you pass in the\n`Manager` classes in the constructor of the layer you're setting.\n\nFor example, here's how to set up a click listener for the KML layer: \n\n### Kotlin\n\n```kotlin\nkmlPolylineLayer.addLayerToMap()\nkmlPolylineLayer.setOnFeatureClickListener { feature: Feature -\u003e\n Toast.makeText(context,\n \"KML polyline clicked: ${feature.getProperty(\"name\")}\",\n Toast.LENGTH_SHORT\n ).show()\n}\n\n \n```\n\n### Java\n\n```java\nkmlPolylineLayer.addLayerToMap();\nkmlPolylineLayer.setOnFeatureClickListener(feature -\u003e Toast.makeText(context,\n \"KML polyline clicked: \" + feature.getProperty(\"name\"),\n Toast.LENGTH_SHORT).show());\n\n \n```\n\n\nWhen you add your own markers, ground overlays, polylines, or polygons, just be sure to add click\nlisteners to those `Collection` objects. For example, here's how to set up the marker\nclick listener on the `markerCollection`: \n\n### Kotlin\n\n```kotlin\nmarkerCollection.setOnMarkerClickListener { marker: Marker -\u003e\n Toast.makeText(\n context,\n \"Marker clicked: ${marker.title}\",\n Toast.LENGTH_SHORT\n ).show()\n false\n}\n\n \n```\n\n### Java\n\n```java\nmarkerCollection.setOnMarkerClickListener(marker -\u003e { Toast.makeText(context,\n \"Marker clicked: \" + marker.getTitle(),\n Toast.LENGTH_SHORT).show();\n return false;\n});\n\n \n```\n\nSee the demo app\n----------------\n\nFor an example of adding multiple layers, take a look at the `MultiLayerDemoActivity`\nin the demo app that ships with the utility library. The [setup guide](/maps/documentation/android-sdk/utility/setup) shows you how to run\nthe demo app."]]