Klickereignisse verarbeiten
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Plattform auswählen:
Android
iOS
JavaScript
Sie können eine Feature-Ebene so konfigurieren, dass auf click
-Ereignisse des Nutzers reagiert wird, um die Orts-ID und den Featuretyp der angeklickten Grenze zu erhalten. Auf der folgenden Beispielkarte sehen Sie Grenzen für die Länderebene. Sie enthält einen Event-Handler, mit dem das angeklickte Polygon, das mit dem ausgewählten Land verknüpft ist, lila ausgefüllt wird.
Click-Event-Handler schreiben
Wenn ein Klickereignis in einer Feature-Ebene auftritt, übergibt das Maps SDK for Android ein FeatureClickEvent
-Objekt an den Event-Handler. Mit FeatureClickEvent
können Sie die Breiten- und Längengradkoordinaten des Klicks sowie eine Liste der vom Klick betroffenen Funktionen abrufen.
Ereignisse auf Feature-Ebene verarbeiten
So behandeln Sie Ereignisse auf Feature-Ebene: In diesem Beispiel definieren Sie einen Event-Handler für Klicks für den Feature-Layer „Country“, um das Polygon, das das ausgewählte Land darstellt, rot zu füllen.
Wenn Sie FeatureLayer.setFeatureStyle()
aufrufen, wird mit der Stil-Factory-Funktion der Stil für alle Elemente auf der Elementebene festgelegt. Wenn Sie den Stil eines Elements im Ereignishandler aktualisieren möchten, müssen Sie FeatureLayer.setFeatureStyle()
aufrufen, um den aktualisierten Stil für alle Elemente festzulegen.
Falls noch nicht geschehen, erstellen Sie eine neue Karten-ID und einen neuen Kartenstil. Folgen Sie dazu der Anleitung unter Erste Schritte. Aktivieren Sie auf jeden Fall die Element-Ebene Land.
Achten Sie darauf, dass Ihre Klasse FeatureLayer.OnFeatureClickListener
implementiert.
Um einen Event-Handler für Feature-Klickereignisse zu registrieren, rufen Sie FeatureLayer.addOnFeatureClickListener()
auf.
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)
}
Wenden Sie auf das ausgewählte Land die Füllfarbe Rot an. Nur sichtbare Features sind anklickbar.
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
}
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
Zuletzt aktualisiert: 2025-08-31 (UTC).
[null,null,["Zuletzt aktualisiert: 2025-08-31 (UTC)."],[[["\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"]]