คุณสามารถทำให้เลเยอร์ของฟีเจอร์ตอบสนองต่อเหตุการณ์ click ของผู้ใช้เพื่อรับรหัสสถานที่และประเภทฟีเจอร์สำหรับขอบเขตที่คลิก แผนที่ตัวอย่างต่อไปนี้แสดงขอบเขตสำหรับเลเยอร์ประเทศ และแสดงเครื่องจัดการเหตุการณ์ที่จัดรูปแบบรูปหลายเหลี่ยมที่คลิกซึ่งเชื่อมโยงกับประเทศที่เลือก
เขียนตัวแฮนเดิลกิจกรรมการคลิก
เมื่อเกิดเหตุการณ์การคลิกในเลเยอร์ฟีเจอร์ Maps SDK สำหรับ Android จะส่ง
FeatureClickEvent
ออบเจ็กต์ไปยังเครื่องจัดการเหตุการณ์ ใช้ FeatureClickEvent เพื่อรับพิกัดละติจูดและลองจิจูดของการคลิก รวมถึงรายการฟีเจอร์ที่ได้รับผลกระทบจากการคลิก
จัดการเหตุการณ์เลเยอร์ของฟีเจอร์
ทำตามขั้นตอนต่อไปนี้เพื่อจัดการเหตุการณ์ในเลเยอร์ฟีเจอร์ ในตัวอย่างนี้ คุณจะกำหนดตัวแฮนเดิลกิจกรรมการคลิกสำหรับเลเยอร์ของฟีเจอร์ประเทศเพื่อใช้สีเติมสีแดงกับรูปหลายเหลี่ยมที่แสดงประเทศที่เลือก
เมื่อคุณเรียกใช้
FeatureLayer.setFeatureStyle(),
ฟังก์ชัน Factory ของสไตล์จะกำหนดสไตล์ให้กับฟีเจอร์ทั้งหมดใน
เลเยอร์ฟีเจอร์ หากต้องการอัปเดตสไตล์ของฟีเจอร์ในตัวแฮนเดิลเหตุการณ์ คุณต้องเรียกใช้ FeatureLayer.setFeatureStyle() เพื่อกำหนดสไตล์ที่อัปเดตให้กับฟีเจอร์ทั้งหมด
หากยังไม่ได้ดำเนินการ ให้ทำตามขั้นตอนใน เริ่มต้นใช้งาน เพื่อสร้างรหัสแผนที่และสไตล์แผนที่ใหม่ อย่าลืมเปิดใช้เลเยอร์ของฟีเจอร์ประเทศ
ตรวจสอบว่าคลาสของคุณใช้
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 }