您可以讓地圖項目圖層回應使用者的 click
事件,以便取得所點擊邊界的地點 ID 和地圖項目類型。以下地圖範例顯示國家/地區圖層的邊界,並顯示事件處理常式,用於設定已點選的多邊形樣式,並與所選國家/地區相關聯。
編寫點擊事件處理常式
地圖項目圖層上發生點擊事件時,Maps SDK for Android 會將 FeatureClickEvent
物件傳遞至事件處理常式。使用 FeatureClickEvent
取得點擊的經緯度座標,以及受到點擊影響的地圖項目清單。
處理地圖項目圖層事件
請按照下列步驟處理地圖項目圖層上的事件。在這個範例中,您會為「Country」地圖項目圖層定義點擊事件處理常式,將紅色填充圖案套用至代表所選國家/地區的多邊形。
呼叫 FeatureLayer.setFeatureStyle()
時,樣式工廠函式會為地圖項目圖層的所有地圖項目設定樣式。如要在事件處理常式中更新地圖項目的樣式,您必須呼叫 FeatureLayer.setFeatureStyle()
,為所有地圖項目設定更新後的樣式。
如果尚未建立新的地圖 ID 和地圖樣式,請按照「開始使用」一文中的步驟進行。請務必啟用「Country」地圖項目圖層。
請確認您的類別實作
FeatureLayer.OnFeatureClickListener
。呼叫
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) }將紅色的填滿顏色套用至所選國家/地區。只有可見的地圖項目可供點選。
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 }