Cómo controlar eventos de clic

Selecciona la plataforma: Android iOS JavaScript

Puedes hacer que una capa de componentes responda a los eventos de click de los usuarios para obtener el lugar ID y tipo de componente del límite en el que se hizo clic. El El siguiente mapa de ejemplo muestra los límites de la capa de país y muestra un controlador de eventos que define el diseño del polígono en el que se hizo clic asociado al el país seleccionado.

Cómo escribir un controlador de eventos de clic

Cuando se produce un evento de clic en una capa de componentes, el SDK de Maps para Android pasa un FeatureClickEvent al controlador de eventos. Usa FeatureClickEvent para obtener la latitud y las coordenadas de longitud del clic y una lista de los elementos afectados por el o hacer clic.

Controla los eventos de la capa de componentes

Sigue estos pasos para controlar los eventos en una capa de componentes. En este ejemplo, defines un controlador de eventos de clic para que la capa del componente País aplique un color rojo. rellenar hasta el polígono que representa el país seleccionado.

Cuando llames FeatureLayer.setFeatureStyle(), la función Style Factory establece el estilo en todas las funciones del capa de componentes. Para actualizar el estilo de un elemento en el controlador de eventos, debes llama a FeatureLayer.setFeatureStyle() para configurar el estilo actualizado en todos atributos.

  1. Si aún no lo has hecho, sigue los pasos en Primeros pasos para crear un nuevo ID y diseño de mapa. Asegúrate de habilitar el campo País. capa de componentes.

  2. Asegúrate de que tu clase implemente FeatureLayer.OnFeatureClickListener

  3. Llama a un controlador de eventos para registrar eventos de clic de funciones 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. Aplica un color de relleno rojo al país seleccionado. Solo se muestran los componentes visibles en el que se puede hacer clic.

    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 }