يمكنك جعل طبقة معالم تستجيب لأحداث click
التي ينفّذها المستخدم للحصول على معرّف المكان ونوع المعلم للحدود التي تم النقر عليها. تعرض خريطة المثال التالية حدود طبقة البلد، كما تعرض معالج أحداث يضبط نمط المضلّع الذي تم النقر عليه والمرتبط بالبلد المحدّد.
كتابة معالج حدث النقر
عند وقوع حدث نقر على طبقة ميزات، تمرِّر حزمة تطوير البرامج بالاستناد إلى بيانات "خرائط Google" لنظام التشغيل Android عنصر
FeatureClickEvent
إلى معالج الأحداث. استخدِم FeatureClickEvent
للحصول على إحداثيات خطوط الطول والعرض للنقرة وقائمة بالعناصر المتأثرة بها.
التعامل مع أحداث طبقة المعالم
اتّبِع الخطوات التالية للتعامل مع الأحداث في طبقة معالم. في هذا المثال، يمكنك تحديد معالج أحداث النقر لطبقة معالم البلد لتطبيق تعبئة حمراء على المضلّع الذي يمثّل البلد المحدّد.
عند استدعاء
FeatureLayer.setFeatureStyle()
،
تضبط دالة إنشاء الأنماط النمط على جميع المعالم في
طبقة المعالم. لتعديل نمط عنصر في معالج الأحداث، عليك استدعاء 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 }