Klickereignisse verarbeiten

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.

  1. 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.

  2. Achten Sie darauf, dass Ihre Klasse FeatureLayer.OnFeatureClickListener implementiert.

  3. 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) }

  4. 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 }