אתם יכולים להגדיר שכבה במפה כך שתגיב לאירועי click של המשתמשים, כדי לקבל את מזהה המקום ואת סוג התכונה של הגבול שעליו המשתמשים לחצו. במפה לדוגמה שבהמשך מוצגים הגבולות של שכבת המדינה, ומוצג גורם מטפל באירועים שמשנה את הסגנון של הפוליגון שנלחץ ומשויך למדינה שנבחרה.
כתיבת גורם מטפל באירועים מסוג קליק
כשמתרחש אירוע מסוג קליק בשכבה במפה, ה-SDK של מפות ל-Android מעביר אובייקט FeatureClickEvent לגורם המטפל באירועים. אפשר להשתמש ב-FeatureClickEvent כדי לקבל את הקואורדינטות של קווי האורך והרוחב של הקליק, ורשימה של התכונות שמושפעות מהקליק.
טיפול באירועים בשכבה במפה
כדי לטפל באירועים בשכבה במפה, פועלים לפי השלבים הבאים. בדוגמה הזו, אתם מגדירים גורם מטפל באירועים מסוג קליק לשכבה במפה של המדינה, כדי להחיל מילוי אדום על הפוליגון שמייצג את המדינה שנבחרה.
כשקוראים לפונקציה FeatureLayer.setFeatureStyle(), פונקציית ה-factory של הסגנון מגדירה את הסגנון בכל הישויות בשכבה במפה. כדי לעדכן את הסגנון של תכונה בגורם המטפל באירועים, צריך להפעיל את FeatureLayer.setFeatureStyle() כדי להגדיר את הסגנון המעודכן בכל התכונות.
אם עדיין לא עשיתם זאת, בצעו את השלבים שמפורטים בקטע תחילת העבודה כדי ליצור מזהה מפה וסגנון מפה חדשים. חשוב להפעיל את השכבה במפה 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 }