处理点击事件

请选择平台: Android iOS JavaScript

您可以让地图项图层响应用户 click 事件,以获取所点击边界的地点 ID 和地图项类型。下面这个示例地图展示了“国家/地区”图层的边界,该地图使用了事件处理脚本,用于为所点击的与所选国家/地区相关联的多边形应用样式。

编写点击事件处理脚本

当地图项图层上发生点击事件时,Maps SDK for Android 会将 FeatureClickEvent 对象传递给事件处理脚本。使用 FeatureClickEvent 获取点击的纬度和经度坐标,以及受点击影响的地图项列表。

处理地图项图层事件

如需处理地图项图层上的事件,请按以下步骤操作。在此示例中,您将为“Country”地图项图层定义点击事件处理脚本,以便对表示所选国家/地区的多边形应用红色填充。

当您调用 FeatureLayer.setFeatureStyle() 时,样式工厂函数会为地图项图层中的所有地图项设置样式。如需在事件处理脚本中更新地图项的样式,您必须调用 FeatureLayer.setFeatureStyle() 以便为所有地图项设置更新后的样式。

  1. 按照开始使用中的步骤创建新的地图 ID 和地图样式(如果您尚未执行此操作)。请务必启用国家/地区地图项图层。

  2. 确保您的类实现了 FeatureLayer.OnFeatureClickListener

  3. 通过调用 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. 为所选国家/地区应用红色的填充颜色。只有可见地图项可供点击。

    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 }