privatevardatasetLayer:FeatureLayer?=null// The globalid of the clicked dataset feature.varlastGlobalId:String?=null overridefunonMapReady(googleMap:GoogleMap){// Get the DATASET feature layer.datasetLayer=googleMap.getFeatureLayer(FeatureLayerOptions.Builder().featureType(FeatureType.DATASET)// Specify the dataset ID..datasetId(YOUR_DATASET_ID).build()) // Register the click event handler for the Datasets layer.datasetLayer?.addOnFeatureClickListener(this) // Apply default style to all features on load to enable clicking.styleDatasetsLayer()} // Define the click event handler to set lastGlobalId to globalid of selected feature.overridefunonFeatureClick(event:FeatureClickEvent){// Get the dataset feature affected by the click.valclickFeatures:MutableList<Feature>=event.featureslastGlobalId=nullif(clickFeatures.get(0)isDatasetFeature){lastGlobalId=((clickFeatures.get(0)asDatasetFeature).getDatasetAttributes().get("globalid"))// Remember to reset the Style Factory.styleDatasetsLayer()}}
Java
privateFeatureLayerdatasetLayer;// The globalid of the clicked dataset feature.Stringlastgobalid=null; @OverridepublicvoidonMapReady(GoogleMapmap){ // Get the DATASET feature layer.datasetLayer=map.getFeatureLayer(newFeatureLayerOptions.Builder().featureType(FeatureType.DATASET)// Specify the dataset ID..datasetId(YOUR_DATASET_ID).build()); // Register the click event handler for the Datasets layer.datasetLayer.addOnFeatureClickListener(this); // Apply default style to all features on load to enable clicking.styleDatasetsLayer();} @Override// Define the click event handler.publicvoidonFeatureClick(FeatureClickEventevent){// Get the dataset feature affected by the click.List<Feature>clickFeatures=event.getFeatures();lastgobalid=null;if(clickFeatures.get(0)instanceofDatasetFeature){lastgobalid=((DatasetFeature)clickFeatures.get(0)).getDatasetAttributes().get("globalid");// Remember to reset the Style Factory.styleDatasetsLayer();}}
将所选地图项的填充颜色设为蓝色,并将所有其他地图项的填充颜色设为绿色。只有可见地图项可供点击。
Kotlin
// Set fill and border for all features.privatefunstyleDatasetsLayer(){// Create the style factory function.valstyleFactory=FeatureLayer.StyleFactory{feature:Feature-> // Check if the feature is an instance of DatasetFeature.if(featureisDatasetFeature){valglobalIDs:MutableMap<String,String>=feature.getDatasetAttributes()// Determine globalid attribute.valglobalID=globalIDs!!["globalid"]// Set default colors to to green.varfillColor=0x800000ffvarstrokeColor=0xff0000ffif(globalID==lastGlobalId){// Color selected area blue.fillColor=0x8000ff00strokeColor=0xff00ff00}return@StyleFactoryFeatureStyle.Builder().fillColor(fillColor).strokeColor(strokeColor).build()}return@StyleFactorynull} // Apply the style factory function to the dataset feature layer.datasetLayer?.setFeatureStyle(styleFactory)}
Java
// Set default green fill and border for all features.privatevoidstyleDatasetsLayer(){// Create the style factory function.FeatureLayer.StyleFactorystyleFactory=(Featurefeature)->{ // Check if the feature is an instance of DatasetFeature.if(featureinstanceofDatasetFeature){ // Check if "globalid" attribute of feature is the "globalid" of clicked feature.Map<String,String>globalIDs=((DatasetFeature)feature).getDatasetAttributes();StringglobalID=globalIDs.get("globalid");// Set default colors to green.intfillColor=0x4000ff00;intstrokeColor=0xff00ff00;if(Objects.equals(globalID,lastgobalid)){// Color selected area blue.fillColor=0x400000ff;strokeColor=0xff0000ff;}returnnewFeatureStyle.Builder().fillColor(fillColor).strokeColor(strokeColor).strokeWidth(2).build();}returnnull;}; // Apply the style factory function to the feature layer.datasetLayer.setFeatureStyle(styleFactory);}
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThis guide explains how to make data features on your map clickable and responsive to user interaction, allowing you to change a feature's appearance upon click.\u003c/p\u003e\n"],["\u003cp\u003eIt involves writing a click event handler that captures the clicked feature's information and uses it to apply styling changes.\u003c/p\u003e\n"],["\u003cp\u003eYou will learn to use the \u003ccode\u003eFeatureClickEvent\u003c/code\u003e to get the clicked features and \u003ccode\u003eFeatureLayer.setFeatureStyle()\u003c/code\u003e to update their appearance, highlighting the selected feature.\u003c/p\u003e\n"],["\u003cp\u003eThe guide includes Android code examples demonstrating how to register the event handler and apply a blue fill to the selected feature, while keeping others green.\u003c/p\u003e\n"]]],["The core content explains how to make data features clickable and dynamically change their appearance upon user interaction within the Android Maps SDK. Key actions include: registering a click event handler via `FeatureLayer.addOnFeatureClickListener()`, retrieving the affected features through `FeatureClickEvent.getFeatures()`, and using `FeatureLayer.setFeatureStyle()` with a custom style factory to apply visual changes. The `globalid` is used to identify and update the style of a selected feature, changing it from a default green to blue.\n"],null,["Select platform: [Android](/maps/documentation/android-sdk/dds-datasets/make-data-features-clickable \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/dds-datasets/make-data-features-tappable \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/dds-datasets/make-data-features-clickable \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nMake data features respond to `click` events, and use them to\nchange the appearance of a feature based on user interaction.\n\nWrite a click event handler\n\nWhen a click event occurs on a feature layer, the Maps SDK for Android passes a\n[`FeatureClickEvent`](/android/reference/com/google/android/gms/maps/model/FeatureClickEvent)\nobject to the event handler.\n\nUse the `FeatureClickEvent.getFeatures()` method to get the list of features\naffected by the click.\n\nHandle feature layer events\n\nTake the following steps to handle events on the Datasets layer. In this\nexample, you apply a blue fill and border to the polygon representing the\nselected feature.\n| **Note:** In this example, the `globalid` attribute of the dataset contains a unique identifier for each polygon. You can then examine the `globalid` of the polygon that was clicked to apply the blue styling only to that polygon.\n\nWhen you call\n[`FeatureLayer.setFeatureStyle()`](/android/reference/com/google/android/gms/maps/model/FeatureLayer#setFeatureStyle(com.google.android.gms.maps.model.FeatureLayer.StyleFactory)),\nthe style factory function sets the feature style on all features in the\ndataset. To update the style of a dataset in the event handler, you must\ncall `FeatureLayer.setFeatureStyle()` to set the updated style on all dataset\nfeatures.\n\n1. If you haven't already done so, follow the steps in\n [Get Started](/maps/documentation/android/dds-datasets/start)\n to create a new map ID and map style. Be sure to enable the **Datasets**\n feature layer.\n\n2. Make sure your class implements\n [`FeatureLayer.OnFeatureClickListener`](/android/reference/com/google/android/gms/maps/model/FeatureLayer.OnFeatureClickListener).\n\n3. Register an event handler for feature click events by calling\n [`FeatureLayer.addOnFeatureClickListener()`](/android/reference/com/google/android/gms/maps/model/FeatureLayer#addOnFeatureClickListener(com.google.android.gms.maps.model.FeatureLayer.OnFeatureClickListener)).\n\n Kotlin\n\n\n ```java\n private var datasetLayer: FeatureLayer? = null\n // The globalid of the clicked dataset feature.\n var lastGlobalId: String? = null\n\n override fun onMapReady(googleMap: GoogleMap) {\n // Get the DATASET feature layer.\n datasetLayer = googleMap.getFeatureLayer(FeatureLayerOptions.Builder()\n .featureType(FeatureType.DATASET)\n // Specify the dataset ID.\n .datasetId(YOUR_DATASET_ID)\n .build())\n\n // Register the click event handler for the Datasets layer.\n datasetLayer?.addOnFeatureClickListener(this)\n\n // Apply default style to all features on load to enable clicking.\n styleDatasetsLayer()\n }\n\n // Define the click event handler to set lastGlobalId to globalid of selected feature.\n override fun onFeatureClick(event: FeatureClickEvent) {\n // Get the dataset feature affected by the click.\n val clickFeatures: MutableList\u003cFeature\u003e = event.features\n lastGlobalId = null\n if (clickFeatures.get(0) is DatasetFeature) {\n lastGlobalId = ((clickFeatures.get(0) as DatasetFeature).getDatasetAttributes().get(\"globalid\"))\n // Remember to reset the Style Factory.\n styleDatasetsLayer()\n }\n }\n ```\n\n \u003cbr /\u003e\n\n Java\n\n\n ```java\n private FeatureLayer datasetLayer;\n // The globalid of the clicked dataset feature.\n String lastgobalid = null;\n\n @Override\n public void onMapReady(GoogleMap map) {\n\n // Get the DATASET feature layer.\n datasetLayer = map.getFeatureLayer(new FeatureLayerOptions.Builder()\n .featureType(FeatureType.DATASET)\n // Specify the dataset ID.\n .datasetId(YOUR_DATASET_ID)\n .build());\n\n // Register the click event handler for the Datasets layer.\n datasetLayer.addOnFeatureClickListener(this);\n\n // Apply default style to all features on load to enable clicking.\n styleDatasetsLayer();\n }\n\n @Override\n // Define the click event handler.\n public void onFeatureClick(FeatureClickEvent event) {\n // Get the dataset feature affected by the click.\n List\u003cFeature\u003e clickFeatures = event.getFeatures();\n lastgobalid = null;\n if (clickFeatures.get(0) instanceof DatasetFeature) {\n lastgobalid = ((DatasetFeature) clickFeatures.get(0)).getDatasetAttributes().get(\"globalid\");\n // Remember to reset the Style Factory.\n styleDatasetsLayer();\n }\n }\n ```\n\n \u003cbr /\u003e\n\n4. Apply a fill color of blue to the selected feature and green to all other\n features. Only visible features are clickable.\n\n Kotlin\n\n\n ```java\n // Set fill and border for all features.\n private fun styleDatasetsLayer() {\n // Create the style factory function.\n val styleFactory = FeatureLayer.StyleFactory { feature: Feature -\u003e\n\n // Check if the feature is an instance of DatasetFeature.\n if (feature is DatasetFeature) {\n val globalIDs: MutableMap\u003cString, String\u003e = feature.getDatasetAttributes()\n // Determine globalid attribute.\n val globalID = globalIDs!![\"globalid\"]\n // Set default colors to to green.\n var fillColor = 0x800000ff\n var strokeColor = 0xff0000ff\n if (globalID == lastGlobalId) {\n // Color selected area blue.\n fillColor = 0x8000ff00\n strokeColor = 0xff00ff00\n }\n return@StyleFactory FeatureStyle.Builder()\n .fillColor(fillColor)\n .strokeColor(strokeColor)\n .build()\n }\n return@StyleFactory null\n }\n\n // Apply the style factory function to the dataset feature layer.\n datasetLayer?.setFeatureStyle(styleFactory)\n }\n ```\n\n \u003cbr /\u003e\n\n Java\n\n\n ```java\n // Set default green fill and border for all features.\n private void styleDatasetsLayer() {\n // Create the style factory function.\n FeatureLayer.StyleFactory styleFactory = (Feature feature) -\u003e {\n\n // Check if the feature is an instance of DatasetFeature.\n if (feature instanceof DatasetFeature) {\n\n // Check if \"globalid\" attribute of feature is the \"globalid\" of clicked feature.\n Map\u003cString, String\u003e globalIDs = ((DatasetFeature) feature).getDatasetAttributes();\n String globalID = globalIDs.get(\"globalid\");\n // Set default colors to green.\n int fillColor = 0x4000ff00;\n int strokeColor = 0xff00ff00;\n if (Objects.equals(globalID, lastgobalid)) {\n // Color selected area blue.\n fillColor = 0x400000ff;\n strokeColor = 0xff0000ff;\n }\n return new FeatureStyle.Builder()\n .fillColor(fillColor)\n .strokeColor(strokeColor)\n .strokeWidth(2)\n .build();\n }\n return null;\n };\n\n // Apply the style factory function to the feature layer.\n datasetLayer.setFeatureStyle(styleFactory);\n }\n ```\n\n \u003cbr /\u003e"]]