Xử lý các sự kiện nhấp chuột

Chọn nền tảng: Android iOS JavaScript

Bạn có thể khiến một lớp đối tượng phản hồi các sự kiện click của người dùng để lấy mã địa điểm và loại đối tượng cho ranh giới đã nhấp. Bản đồ ví dụ sau đây cho thấy ranh giới của lớp Quốc gia và trình xử lý sự kiện tạo kiểu cho đa giác đã nhấp được liên kết với quốc gia đã chọn.

Viết trình xử lý sự kiện nhấp chuột

Khi một sự kiện nhấp chuột xảy ra trên một lớp đối tượng, Maps SDK cho Android sẽ truyền một FeatureClickEvent đối tượng đến trình xử lý sự kiện. Sử dụng FeatureClickEvent để lấy toạ độ vĩ độ và kinh độ của lượt nhấp và danh sách các đối tượng chịu ảnh hưởng của lượt nhấp.

Xử lý các sự kiện lớp đối tượng

Hãy làm theo các bước sau để xử lý các sự kiện trên một lớp đối tượng. Trong ví dụ này, bạn sẽ xác định trình xử lý sự kiện nhấp cho lớp đối tượng Quốc gia để áp dụng màu đỏ cho đa giác đại diện cho quốc gia đã chọn.

Khi bạn gọi FeatureLayer.setFeatureStyle(), hàm nhà máy kiểu sẽ đặt kiểu cho tất cả các đối tượng trong lớp đối tượng. Để cập nhật kiểu của một đối tượng trong trình xử lý sự kiện, bạn phải gọi FeatureLayer.setFeatureStyle() để đặt kiểu đã cập nhật cho tất cả các đối tượng.

  1. Nếu bạn chưa làm như vậy, hãy làm theo các bước trong Bắt đầu để tạo mã bản đồ và kiểu bản đồ mới. Nhớ bật lớp đối tượng Quốc gia.

  2. Đảm bảo lớp của bạn triển khai FeatureLayer.OnFeatureClickListener.

  3. Đăng ký trình xử lý sự kiện cho các sự kiện nhấp vào đối tượng bằng cách gọi 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. Áp dụng màu nền đỏ cho quốc gia đã chọn. Chỉ những đối tượng hiển thị mới có thể nhấp.

    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 }