클릭 이벤트 처리

플랫폼 선택: Android iOS JavaScript

지형지물 레이어가 사용자 click 이벤트에 응답하여 장소를 가져오도록 할 수 있습니다. 클릭된 경계의 ID 및 지형지물 유형 이 다음 지도 예에서는 국가 레이어의 경계를 보여줍니다. 는 선택할 수 있습니다.

클릭 이벤트 핸들러 작성

지형지물 레이어에서 클릭 이벤트가 발생하면 Android용 Maps SDK는 FeatureClickEvent 드림 객체를 이벤트 핸들러에 추가합니다. FeatureClickEvent를 사용하여 위도 가져오기 클릭의 경도 좌표와 경도 좌표에 의해 영향을 받는 지형지물의 목록을 클릭합니다.

지형지물 레이어 이벤트 처리

다음 단계에 따라 지형지물 레이어에서 이벤트를 처리합니다. 이 예에서 국가 지형지물 레이어의 클릭 이벤트 핸들러를 정의하여 빨간색 선택한 국가를 나타내는 다각형에 채울 수 있습니다.

통화 시 FeatureLayer.setFeatureStyle(), 스타일 팩토리 함수는 지형지물 레이어입니다. 이벤트 핸들러에서 지형지물의 스타일을 업데이트하려면 FeatureLayer.setFeatureStyle()를 호출하여 모든 광고 항목에 업데이트된 스타일을 설정합니다. 기능을 살펴보겠습니다

  1. 아직 시작하지 않았다면 다음 단계를 따르세요. 시작하기 새 지도 ID와 지도 스타일을 만듭니다. 국가 옵션을 사용 설정해야 합니다. 지형지물 레이어입니다.

  2. 클래스가 구현되었는지 확인 FeatureLayer.OnFeatureClickListener

  3. 다음을 호출하여 기능 클릭 이벤트에 이벤트 핸들러를 등록합니다. FeatureLayer.addOnFeatureClickListener()

    자바

    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. 선택한 국가에 빨간색 채우기 색상을 적용합니다. 표시되는 지형지물만 있습니다.

    자바

    @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 }