میتوانید لایه عارضه را طوری تنظیم کنید که به رویدادهای click کاربر پاسخ دهد تا شناسه مکان و نوع عارضه برای مرزی که روی آن کلیک شده است دریافت شود. نقشه نمونه زیر مرزهای لایه «کشور» را نشان میدهد و یک گرداننده رویداد را نشان میدهد که به چندضلعی کلیکشده مرتبط با کشور انتخابشده سبک میدهد.
نوشتن گرداننده رویداد کلیک
وقتی رویداد کلیک در لایه عارضه رخ میدهد، «کیت توسعه نرمافزار Google Maps برای Android» شیء
FeatureClickEvent
را به گرداننده رویداد ارسال میکند. از FeatureClickEvent برای دریافت مختصات طول و عرض جغرافیایی کلیک و فهرستی از ویژگیهای تحتتأثیر کلیک استفاده کنید.
مدیریت رویدادهای لایه عارضه
برای رسیدگی به رویدادها در لایه عارضه، مراحل زیر را دنبال کنید. در این مثال، یک کنترلکننده رویداد کلیک برای لایه عارضه «کشور» تعریف میکنید تا رنگ قرمز را به چندضلعی نشاندهنده کشور انتخابشده اعمال کند.
وقتی
FeatureLayer.setFeatureStyle() را فرامیخوانید،
تابع کارخانه سبک، سبک را روی همه عارضهها در
لایه عارضه تنظیم میکند. برای بهروزرسانی سبک عارضه در گرداننده رویداد، باید
FeatureLayer.setFeatureStyle() را فراخوانی کنید تا سبک بهروزشده را در همه
عارضهها تنظیم کنید.
اگر قبلاً این کار را نکردهاید، مراحل شروع به کار را برای ایجاد شناسه نقشه و سبک نقشه جدید دنبال کنید. حتماً لایه عارضه کشور را فعال کنید.
مطمئن شوید که کلاس شما
FeatureLayer.OnFeatureClickListenerرا پیادهسازی میکند.با فراخوانی
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) }رنگ پرکننده قرمز را برای کشور انتخابی اعمال کنید. فقط عارضههای قابلمشاهده کلیککردنی هستند.
جاوا
@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 }