处理点击事件
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以让地图项图层响应用户 click
事件,以获取所点击边界的地点 ID 和地图项类型。下面这个示例地图展示了“国家/地区”图层的边界,该地图使用了事件处理脚本,用于为与所选国家/地区相关联的所点击多边形应用样式。
编写点击事件处理脚本
当地图项图层上发生点击事件时,Maps SDK for Android 会将 FeatureClickEvent
对象传递给事件处理脚本。使用 FeatureClickEvent
可获取点击的纬度和经度坐标,以及受点击影响的要素列表。
处理地图项图层事件
如需处理地图项图层上的事件,请按以下步骤操作。在此示例中,您为“国家/地区”要素图层定义了一个点击事件处理脚本,用于将红色填充应用于表示所选国家/地区的多边形。
当您调用 FeatureLayer.setFeatureStyle()
时,样式工厂函数会为地图项图层中的所有地图项设置样式。如需在事件处理脚本中更新地图项的样式,您必须调用 FeatureLayer.setFeatureStyle()
以设置所有地图项的更新后样式。
按照开始使用中的步骤创建新的地图 ID 和地图样式(如果您尚未执行此操作)。请务必启用 Country(国家/地区)地图项图层。
确保您的类实现了 FeatureLayer.OnFeatureClickListener
。
通过调用 FeatureLayer.addOnFeatureClickListener()
为要素点击事件注册事件处理脚本。
Java
private FeatureLayer countryLayer;
@Override
public void onMapReady(GoogleMap map) {
// Get the COUNTRY feature layer.
countryLayer = map.getFeatureLayer(new FeatureLayerOptions.Builder()
.featureType(FeatureType.COUNTRY)
.build());
// Register the click event handler for the Country layer.
countryLayer.addOnFeatureClickListener(this);
// Apply default style to all countries on load to enable clicking.
styleCountryLayer();
}
// Set default fill and border for all countries to ensure that they respond
// to click events.
private void styleCountryLayer() {
FeatureLayer.StyleFactory styleFactory = (Feature feature) -> {
return new FeatureStyle.Builder()
// Set the fill color for the country as white with a 10% opacity.
.fillColor(Color.argb(0.1, 0, 0, 0))
// Set border color to solid black.
.strokeColor(Color.BLACK)
.build();
};
// Apply the style factory function to the country feature layer.
countryLayer.setFeatureStyle(styleFactory);
}
Kotlin
private var countryLayer: FeatureLayer? = null
override fun onMapReady(googleMap: GoogleMap) {
// Get the COUNTRY feature layer.
countryLayer = googleMap.getFeatureLayer(FeatureLayerOptions.Builder()
.featureType(FeatureType.COUNTRY)
.build())
// Register the click event handler for the Country layer.
countryLayer?.addOnFeatureClickListener(this)
// Apply default style to all countries on load to enable clicking.
styleCountryLayer()
}
// Set default fill and border for all countries to ensure that they respond
// to click events.
private fun styleCountryLayer() {
val styleFactory = FeatureLayer.StyleFactory { feature: Feature ->
return@StyleFactory FeatureStyle.Builder()
// Set the fill color for the country as white with a 10% opacity.
.fillColor(Color.argb(0.1f, 0f, 0f, 0f))
// Set border color to solid black.
.strokeColor(Color.BLACK)
.build()
}
// Apply the style factory function to the country feature layer.
countryLayer?.setFeatureStyle(styleFactory)
}
为所选国家/地区应用红色填充颜色。只有可见地图项可供点击。
Java
@Override
// Define the click event handler.
public void onFeatureClick(FeatureClickEvent event) {
// Get the list of features affected by the click using
// getPlaceIds() defined below.
List<String> selectedPlaceIds = getPlaceIds(event.getFeatures());
if (!selectedPlaceIds.isEmpty()) {
FeatureLayer.StyleFactory styleFactory = (Feature feature) -> {
// Use PlaceFeature to get the placeID of the country.
if (feature instanceof PlaceFeature) {
if (selectedPlaceIds.contains(((PlaceFeature) feature).getPlaceId())) {
return new FeatureStyle.Builder()
// Set the fill color to red.
.fillColor(Color.RED)
.build();
}
}
return null;
};
// Apply the style factory function to the feature layer.
countryLayer.setFeatureStyle(styleFactory);
}
}
// Get a List of place IDs from the FeatureClickEvent object.
private List<String> getPlaceIds(List<Feature> features) {
List<String> placeIds = new ArrayList<>();
for (Feature feature : features) {
if (feature instanceof PlaceFeature) {
placeIds.add(((PlaceFeature) feature).getPlaceId());
}
}
return placeIds;
}
Kotlin
// Define the click event handler.
override fun onFeatureClick(event: FeatureClickEvent) {
// Get the list of features affected by the click using
// getPlaceIds() defined below.
val selectedPlaceIds = getPlaceIds(event.getFeatures())
if (!selectedPlaceIds.isEmpty()) {
val styleFactory = FeatureLayer.StyleFactory { feature: Feature ->
// Use PlaceFeature to get the placeID of the country.
if (feature is PlaceFeature) {
if (selectedPlaceIds.contains((feature as PlaceFeature).getPlaceId())) {
return@StyleFactory FeatureStyle.Builder()
// Set the fill color to red.
.fillColor(Color.RED)
.build()
}
}
return@StyleFactory null
}
// Apply the style factory function to the feature layer.
countryLayer?.setFeatureStyle(styleFactory)
}
}
// Get a List of place IDs from the FeatureClickEvent object.
private fun getPlaceIds(features: List<Feature>): List<String> {
val placeIds: MutableList<String> = ArrayList()
for (feature in features) {
if (feature is PlaceFeature) {
placeIds.add((feature as PlaceFeature).getPlaceId())
}
}
return placeIds
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eLearn how to make a feature layer respond to user click events, retrieving place ID and feature type.\u003c/p\u003e\n"],["\u003cp\u003eThis guide provides steps and code examples to create a click event handler for styling clicked boundaries.\u003c/p\u003e\n"],["\u003cp\u003eSee how to register an event handler for feature click events and apply custom styles to selected features.\u003c/p\u003e\n"],["\u003cp\u003eCode samples demonstrate retrieving the latitude/longitude of a click and a list of affected features.\u003c/p\u003e\n"],["\u003cp\u003eUnderstand how to implement a feature click event listener and style a clicked polygon with a red fill.\u003c/p\u003e\n"]]],[],null,["# Handle click events\n\nSelect platform: [Android](/maps/documentation/android-sdk/dds-boundaries/handle-events \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/dds-boundaries/handle-events \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/dds-boundaries/handle-events \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nYou can make a feature layer respond to user `click` events to get the place\nID and feature type for the boundary that was clicked. The\nfollowing example map shows the boundaries for the Country layer, and\nshows an event handler that styles the clicked polygon associated with the\nselected country.\n\nWrite a click event handler\n---------------------------\n\nWhen a click event occurs on a feature layer, the Maps SDK for Android passes a\n[`FeatureClickEvent`](/android/reference/com/google/android/gms/maps/model/FeatureClickEvent)\nobject to the event handler. Use the `FeatureClickEvent` to get the latitude\nand longitude coordinates of the click and a list of features affected by the\nclick.\n\nHandle feature layer events\n---------------------------\n\nTake the following steps to handle events on a feature layer. In this example,\nyou define a click event handler for the Country feature layer to apply a red\nfill to the polygon representing the selected country.\n\nWhen you call\n[`FeatureLayer.setFeatureStyle()`](/android/reference/com/google/android/gms/maps/model/FeatureLayer#setFeatureStyle(com.google.android.gms.maps.model.FeatureLayer.StyleFactory)),\nthe style factory function sets the style on all features in the\nfeature layer. To update the style of a feature in the event handler, you must\ncall `FeatureLayer.setFeatureStyle()` to set the updated style on all\nfeatures.\n\n1. If you haven't already done so, follow the steps in\n [Get Started](/maps/documentation/android/dds-boundaries/start)\n to create a new map ID and map style. Be sure to enable the **Country**\n feature layer.\n\n2. Make sure your class implements\n [`FeatureLayer.OnFeatureClickListener`](/android/reference/com/google/android/gms/maps/model/FeatureLayer.OnFeatureClickListener).\n\n3. Register an event handler for feature click events by calling\n [`FeatureLayer.addOnFeatureClickListener()`](/android/reference/com/google/android/gms/maps/model/FeatureLayer#addOnFeatureClickListener(com.google.android.gms.maps.model.FeatureLayer.OnFeatureClickListener)).\n\n ### Java\n\n\n ```java\n private FeatureLayer countryLayer;\n\n @Override\n public void onMapReady(GoogleMap map) {\n\n // Get the COUNTRY feature layer.\n countryLayer = map.getFeatureLayer(new FeatureLayerOptions.Builder()\n .featureType(FeatureType.COUNTRY)\n .build());\n\n // Register the click event handler for the Country layer.\n countryLayer.addOnFeatureClickListener(this);\n\n // Apply default style to all countries on load to enable clicking.\n styleCountryLayer();\n }\n\n // Set default fill and border for all countries to ensure that they respond\n // to click events.\n private void styleCountryLayer() {\n FeatureLayer.StyleFactory styleFactory = (Feature feature) -\u003e {\n return new FeatureStyle.Builder()\n // Set the fill color for the country as white with a 10% opacity.\n .fillColor(Color.argb(0.1, 0, 0, 0))\n // Set border color to solid black.\n .strokeColor(Color.BLACK)\n .build();\n };\n\n // Apply the style factory function to the country feature layer.\n countryLayer.setFeatureStyle(styleFactory);\n }\n ```\n\n \u003cbr /\u003e\n\n ### Kotlin\n\n\n ```java\n private var countryLayer: FeatureLayer? = null\n\n override fun onMapReady(googleMap: GoogleMap) {\n // Get the COUNTRY feature layer.\n countryLayer = googleMap.getFeatureLayer(FeatureLayerOptions.Builder()\n .featureType(FeatureType.COUNTRY)\n .build())\n\n // Register the click event handler for the Country layer.\n countryLayer?.addOnFeatureClickListener(this)\n\n // Apply default style to all countries on load to enable clicking.\n styleCountryLayer()\n }\n\n // Set default fill and border for all countries to ensure that they respond\n // to click events.\n private fun styleCountryLayer() {\n val styleFactory = FeatureLayer.StyleFactory { feature: Feature -\u003e\n return@StyleFactory FeatureStyle.Builder()\n // Set the fill color for the country as white with a 10% opacity.\n .fillColor(Color.argb(0.1f, 0f, 0f, 0f))\n // Set border color to solid black.\n .strokeColor(Color.BLACK)\n .build()\n }\n\n // Apply the style factory function to the country feature layer.\n countryLayer?.setFeatureStyle(styleFactory)\n }\n ```\n\n \u003cbr /\u003e\n\n4. Apply a fill color of red to the selected country. Only visible features are\n clickable.\n\n ### Java\n\n\n ```java\n @Override\n // Define the click event handler.\n public void onFeatureClick(FeatureClickEvent event) {\n\n // Get the list of features affected by the click using\n // getPlaceIds() defined below.\n List\u003cString\u003e selectedPlaceIds = getPlaceIds(event.getFeatures());\n\n if (!selectedPlaceIds.isEmpty()) {\n FeatureLayer.StyleFactory styleFactory = (Feature feature) -\u003e {\n // Use PlaceFeature to get the placeID of the country.\n if (feature instanceof PlaceFeature) {\n if (selectedPlaceIds.contains(((PlaceFeature) feature).getPlaceId())) {\n return new FeatureStyle.Builder()\n // Set the fill color to red.\n .fillColor(Color.RED)\n .build();\n }\n }\n return null;\n };\n\n // Apply the style factory function to the feature layer.\n countryLayer.setFeatureStyle(styleFactory);\n }\n }\n\n // Get a List of place IDs from the FeatureClickEvent object.\n private List\u003cString\u003e getPlaceIds(List\u003cFeature\u003e features) {\n List\u003cString\u003e placeIds = new ArrayList\u003c\u003e();\n for (Feature feature : features) {\n if (feature instanceof PlaceFeature) {\n placeIds.add(((PlaceFeature) feature).getPlaceId());\n }\n }\n return placeIds;\n }\n ```\n\n \u003cbr /\u003e\n\n ### Kotlin\n\n\n ```java\n // Define the click event handler.\n override fun onFeatureClick(event: FeatureClickEvent) {\n\n // Get the list of features affected by the click using\n // getPlaceIds() defined below.\n val selectedPlaceIds = getPlaceIds(event.getFeatures())\n if (!selectedPlaceIds.isEmpty()) {\n val styleFactory = FeatureLayer.StyleFactory { feature: Feature -\u003e\n // Use PlaceFeature to get the placeID of the country.\n if (feature is PlaceFeature) {\n if (selectedPlaceIds.contains((feature as PlaceFeature).getPlaceId())) {\n return@StyleFactory FeatureStyle.Builder()\n // Set the fill color to red.\n .fillColor(Color.RED)\n .build()\n }\n }\n return@StyleFactory null\n }\n\n // Apply the style factory function to the feature layer.\n countryLayer?.setFeatureStyle(styleFactory)\n }\n }\n\n // Get a List of place IDs from the FeatureClickEvent object.\n private fun getPlaceIds(features: List\u003cFeature\u003e): List\u003cString\u003e {\n val placeIds: MutableList\u003cString\u003e = ArrayList()\n for (feature in features) {\n if (feature is PlaceFeature) {\n placeIds.add((feature as PlaceFeature).getPlaceId())\n }\n }\n return placeIds\n }\n ```\n\n \u003cbr /\u003e"]]