Gestire gli eventi relativi ai clic

Seleziona la piattaforma: Android iOS JavaScript

Puoi fare in modo che un livello delle funzionalità risponda agli eventi click dell'utente per ottenere il luogo ID e tipo di elemento per il confine su cui è stato fatto clic. La la mappa di esempio che segue mostra i confini per il livello Paese e mostra un gestore di eventi che definisce lo stile del poligono selezionato associato al nel paese selezionato.

Scrivi un gestore di eventi clic

Quando un evento di clic si verifica a un livello di funzionalità, Maps SDK for Android trasmette un FeatureClickEvent al gestore di eventi. Utilizza FeatureClickEvent per conoscere la latitudine le coordinate di longitudine e longitudine del clic e un elenco degli elementi interessati clic.

Gestire gli eventi del livello delle caratteristiche

Per gestire gli eventi a livello di funzionalità, procedi nel seguente modo. In questo esempio, definisci un gestore di eventi clic per il livello della funzionalità Paese per applicare un riempie il poligono che rappresenta il paese selezionato.

Quando chiami FeatureLayer.setFeatureStyle(), La funzione di fabbrica dello stile imposta lo stile su tutte le caratteristiche in degli elementi. Per aggiornare lo stile di una caratteristica nel gestore di eventi, devi chiama FeatureLayer.setFeatureStyle() per impostare lo stile aggiornato su tutti le funzionalità di machine learning.

  1. Se non lo hai già fatto, segui la procedura descritta in Inizia per creare un nuovo ID mappa e un nuovo stile di mappa. Assicurati di attivare l'opzione Paese. degli elementi.

  2. Assicurati che il corso implementi FeatureLayer.OnFeatureClickListener

  3. Registra un gestore di eventi per gli eventi di clic sulle funzionalità richiamando 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) }

  4. Applica il colore di riempimento rosso al paese selezionato. Solo le funzionalità visibili sono cliccabile.

    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 }