क्लिक इवेंट हैंडल करना

प्लैटफ़ॉर्म चुनें: Android iOS JavaScript

जगह पाने के लिए, फ़ीचर लेयर को उपयोगकर्ता click इवेंट का जवाब दिया जा सकता है क्लिक की गई सीमा का आईडी और सुविधा प्रकार. कॉन्टेंट बनाने उदाहरण के तौर पर दिया गया यह मैप, देश की लेयर की सीमाएं दिखाता है और यह एक इवेंट हैंडलर दिखाता है, जो चुना गया देश.

कोई क्लिक इवेंट हैंडलर लिखें

जब किसी फ़ीचर लेयर पर कोई क्लिक इवेंट होता है, तो Android के लिए Maps SDK FeatureClickEvent ऑब्जेक्ट को इवेंट हैंडलर में डालने की ज़रूरत नहीं है. अक्षांश पाने के लिए FeatureClickEvent इस्तेमाल करें क्लिक के देशांतर निर्देशांक और उन सुविधाओं की सूची जिन पर क्लिक.

सुविधा की लेयर वाले इवेंट मैनेज करना

किसी सुविधा लेयर पर इवेंट मैनेज करने के लिए, नीचे दिया गया तरीका अपनाएं. इस उदाहरण में, तो आप देश सुविधा परत के लिए एक क्लिक इवेंट हैंडलर लागू करने के लिए चुने गए देश को दिखाने वाले पॉलीगॉन भरें.

आपके कॉल करने पर FeatureLayer.setFeatureStyle(), स्टाइल फ़ैक्ट्री फ़ंक्शन, फ़ोटो की सभी सुविधाओं के लिए स्टाइल सेट करता है फ़ीचर लेयर. इवेंट हैंडलर में किसी सुविधा की स्टाइल अपडेट करने के लिए, यह ज़रूरी है कि सभी पर अपडेट की गई स्टाइल सेट करने के लिए, FeatureLayer.setFeatureStyle() को कॉल करें सुविधाएँ.

  1. अगर आपने पहले से ऐसा नहीं किया है, तो इसमें दिए गए चरणों का पालन करें शुरू करना नया मैप आईडी और मैप स्टाइल बनाने के लिए. पक्का करें कि देश का नाम चालू हो फ़ीचर लेयर.

  2. पक्का करें कि आपकी क्लास लागू करती है FeatureLayer.OnFeatureClickListener.

  3. कॉल करके सुविधा क्लिक इवेंट के लिए एक इवेंट हैंडलर रजिस्टर करें 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) }

  4. चुने गए देश के लिए लाल रंग का रंग भरें. सिर्फ़ वे सुविधाएं दिखती हैं जो Google पर दिखती हैं क्लिक करने योग्य.

    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 }