privateFeatureLayerpostalCodeLayer; @OverridepublicvoidonMapReady(GoogleMapmap){// Get the POSTAL_CODE feature layer.postalCodeLayer=map.getFeatureLayer(newFeatureLayerOptions.Builder().featureType(FeatureType.POSTAL_CODE).build());...}
Kotlin
privatevarpostalCodeLayer:FeatureLayer?=null overridefunonMapReady(googleMap:GoogleMap){// Get the POSTAL_CODE feature layer.postalCodeLayer=googleMap.getFeatureLayer(newFeatureLayerOptions.Builder().featureType(FeatureType.POSTAL_CODE).build())...}
边界的数据驱动型样式需要地图 ID。如果缺少地图 ID,或传递的地图 ID 无效,则无法加载边界的数据驱动型样式。作为问题排查步骤,您可以使用 MapCapabilities 检查是否支持边界的数据驱动型样式。
Kotlin
valcapabilities:MapCapabilities=googleMap.getMapCapabilities()System.out.println("Data-driven Styling is available: "+capabilities.isDataDrivenStylingAvailable())
Java
MapCapabilitiescapabilities=googleMap.getMapCapabilities();System.out.println("Data-driven Styling is available: "+capabilities.isDataDrivenStylingAvailable());
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eData-driven styling for boundaries requires using the upgraded map renderer (available since Maps SDK for Android v18.0.0) and creating a map ID with an associated map style.\u003c/p\u003e\n"],["\u003cp\u003eYou can select specific boundary feature layers (like Postal Code or Locality) to display on your map by customizing your map style in the Google Cloud Console, optimizing for performance by choosing only necessary layers.\u003c/p\u003e\n"],["\u003cp\u003eTo apply data-driven styling, get access to feature layers via \u003ccode\u003eGoogleMap.getFeatureLayer()\u003c/code\u003e with \u003ccode\u003eFeatureLayerOptions\u003c/code\u003e defining the desired layer type, and style them individually.\u003c/p\u003e\n"],["\u003cp\u003eBefore implementing data-driven styling, it's crucial to check for device compatibility using \u003ccode\u003eMapCapabilities.isDataDrivenStylingAvailable()\u003c/code\u003e at runtime and provide a fallback in case it's not supported.\u003c/p\u003e\n"]]],[],null,["# Get started\n\nSelect platform: [Android](/maps/documentation/android-sdk/dds-boundaries/start \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/dds-boundaries/start \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/dds-boundaries/start \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nFollow these steps to get set up with data-driven styling for boundaries.\n\nEnable the new maps renderer\n----------------------------\n\nAn upgraded map renderer is available as of version 18.0.0 of the\nMaps SDK for Android. This renderer brings many improvements, including\nsupport for Cloud-based maps styling, to Maps SDK for Android.\n\nWith the release of version 18.2.0 of the Maps SDK for Android, Google switched the\ndefault renderer from the legacy renderer to the upgraded map renderer. This\nchange means that the upgraded map renderer is now used by default in your app\nwhen you build a new app or rebuild an existing app.\n| **Caution:** To use data-driven styling for boundaries in your app, your app must be built using the upgraded map renderer. If you are adding data-driven styling for boundaries to an existing app, ensure that you rebuild the app so that your app uses the upgraded map renderer. For more information, see [New Map Renderer](/maps/documentation/android-sdk/renderer).\n\nCreate a map ID\n---------------\n\nTo create a new map ID, follow the steps at\n[Create a map ID](/maps/documentation/get-map-id#create-a-map-id). Make sure you\nset the Map type to **Android**.\n\nCreate a new map style\n----------------------\n\nTo create a new map style, follow the instructions in [Manage map styles](/maps/documentation/android-sdk/cloud-customization/map-styles)\nto create the style, and [associate the style with the map ID you just created](/maps/documentation/android-sdk/cloud-customization/map-styles#associate-style-with-map-id).\n| **Experimental:** This feature can only be set for light map styles. When you link a light map style that has this feature enabled to a [map\n| ID](../map-ids/mapid-over), the enabled layers are also available for the dark map style.\n\n### Select the feature layers in your new map style\n\nIn the Google API Console you can select which feature layers to display for\nyour new map style. The feature layer determines which kinds of boundaries\nappear on the map (for example localities, states, and so on).\n| **Important:** For optimal performance, only select the layers you need.\n\n1. In the Google API Console, [go to the Map Styles page](https://console.cloud.google.com/project/_/google/maps-apis/studio/styles).\n2. Select a project if prompted.\n3. Select a map style.\n4. Click the **Feature layers** drop-down to add or remove layers.\n5. Click **Save** to save your changes and make them available to your maps.\n\nUpdate your map initialization code\n-----------------------------------\n\nThis step requires the map ID you just created. It can be found on your\n[Maps Management](https://console.cloud.google.com/google/maps-apis/studio/maps) page.\n\nTo add the map ID to your initialization code, see\n[Add the map ID to your app](/maps/documentation/get-map-id#add-a-map-id-to-your-app).\n\nGet access to a feature layer of a map\n--------------------------------------\n\nYou use a [`FeatureLayer`](/android/reference/com/google/android/gms/maps/model/FeatureLayer)\nobject to represent each feature layer of a map, such as a Postal Code or\nLocality feature layer. To get access to a feature layer object, call\n[`GoogleMap.getFeatureLayer()`](/android/reference/com/google/android/gms/maps/GoogleMap#getFeatureLayer(com.google.android.gms.maps.model.FeatureLayerOptions))\nwhen the map initializes: \n\n### Java\n\n\n```javascript\nprivate FeatureLayer postalCodeLayer;\n\n@Override\npublic void onMapReady(GoogleMap map) {\n // Get the POSTAL_CODE feature layer.\n postalCodeLayer = map.getFeatureLayer(new FeatureLayerOptions.Builder()\n .featureType(FeatureType.POSTAL_CODE)\n .build());\n ...\n}\n```\n\n\u003cbr /\u003e\n\n### Kotlin\n\n\n```java\nprivate var postalCodeLayer: FeatureLayer? = null\n\noverride fun onMapReady(googleMap: GoogleMap) {\n // Get the POSTAL_CODE feature layer.\n postalCodeLayer = googleMap.getFeatureLayer(new FeatureLayerOptions.Builder()\n .featureType(FeatureType.POSTAL_CODE)\n .build())\n ...\n}\n```\n\n\u003cbr /\u003e\n\nPass a [`FeatureLayerOptions`](/android/reference/com/google/android/gms/maps/model/FeatureLayerOptions)\nobject to `getFeatureLayer()` to specify the type of the feature layer. In this\nexample, you want to access the `POSTAL_CODE` layer. Create a different\n`FeatureLayer` object for each layer that you want to style.\n\nOnce you have the `FeatureLayer` object, you can apply styling to the\nboundary polygons in that feature.\n| **Note:** If a `FeatureLayer` of the specified type does not exist for the map, or if data-driven styling for boundaries is not enabled because you have not specified the map ID, this call returns but logs an error. Use the [`FeatureLayer.isAvailable()`](/android/reference/com/google/android/gms/maps/model/FeatureLayer#isAvailable()) method to determine if a `FeatureLayer` is available.\n\nCheck map capabilities (required)\n---------------------------------\n\nData-driven styling for boundaries requires a map ID. If the map ID is missing, or an\ninvalid map ID is passed, data-driven styling for boundaries cannot load. As a\ntroubleshooting step, you can use\n[`MapCapabilities`](/android/reference/com/google/android/gms/maps/model/MapCapabilities)\nto check if data-driven styling for boundaries is supported.\n| **Caution:** Some devices might not support the new map renderer and therefore cannot display data-driven styling for boundaries. You must use `MapCapabilities` to check for support at runtime. If data-driven styling for boundaries is not supported, define a fallback option. For device requirements for the new map renderer, see [Supported\ndevices](/maps/documentation/android-sdk/renderer#supported-devices). \n\n### Kotlin\n\n\n```java\nval capabilities: MapCapabilities = googleMap.getMapCapabilities()\nSystem.out.println(\"Data-driven Styling is available: \" + capabilities.isDataDrivenStylingAvailable())\n```\n\n\u003cbr /\u003e\n\n### Java\n\n\n```java\nMapCapabilities capabilities = googleMap.getMapCapabilities();\nSystem.out.println(\"Data-driven Styling is available: \" + capabilities.isDataDrivenStylingAvailable());\n```\n\n\u003cbr /\u003e"]]