如果地點 ID 可用,Android 適用的 Maps SDK 會將 PlaceFeature 的例項傳遞至樣式工廠函式,方便您判斷特徵的位置。
樣式工廠範例
這個範例會將樣式工廠函式套用至縣市地圖項目圖層中的多邊形。樣式工廠函式會使用 PlaceFeature 執行個體,判斷地圖項目的地點 ID。如果地點 ID 是夏威夷哈納,函式會將自訂填滿和筆劃樣式套用至多邊形:
如果尚未建立新的地圖 ID 和地圖樣式,請按照「開始使用」一文中的步驟進行。請務必啟用縣市地圖項目圖層。
在地圖初始化時,取得縣市地圖項目圖層的參照。
Java
privateFeatureLayerlocalityLayer; @OverridepublicvoidonMapReady(GoogleMapmap){// Get the LOCALITY feature layer.localityLayer=map.getFeatureLayer(newFeatureLayerOptions.Builder().featureType(FeatureType.LOCALITY).build()); // Apply style factory function to LOCALITY layer.styleLocalityLayer();}
Kotlin
privatevarlocalityLayer:FeatureLayer?=null overridefunonMapReady(googleMap:GoogleMap){// Get the LOCALITY feature layer.localityLayer=googleMap.getFeatureLayer(FeatureLayerOptions.Builder().featureType(FeatureType.LOCALITY).build()) // Apply style factory function to LOCALITY layer.styleLocalityLayer()}
建立樣式工廠函式,並套用至 Locality 地圖項目圖層。
下例只會在特徵的地點 ID 為夏威夷哈納 (「ChIJ0zQtYiWsVHkRk8lRoB1RNPo」) 時套用函式。如果指定的地點 ID 不是夏威夷哈納,系統就不會套用樣式。
Java
privatevoidstyleLocalityLayer(){ // Create the style factory function.FeatureLayer.StyleFactorystyleFactory=(Featurefeature)->{ // Check if the feature is an instance of PlaceFeature,// which contains a place ID.if(featureinstanceofPlaceFeature){PlaceFeatureplaceFeature=(PlaceFeature)feature; // Determine if the place ID is for Hana, HI.if(placeFeature.getPlaceId().equals("ChIJ0zQtYiWsVHkRk8lRoB1RNPo")){ // Use FeatureStyle.Builder to configure the FeatureStyle object// returned by the style factory function.returnnewFeatureStyle.Builder()// Define a style with purple fill at 50% opacity and solid purple border..fillColor(0x80810FCB).strokeColor(0xFF810FCB).build();}}returnnull;}; // Apply the style factory function to the feature layer.localityLayer.setFeatureStyle(styleFactory);}
Kotlin
privatefunstyleLocalityLayer(){ // Create the style factory function.valstyleFactory=FeatureLayer.StyleFactory{feature:Feature-> // Check if the feature is an instance of PlaceFeature,// which contains a place ID.if(featureisPlaceFeature){valplaceFeature:PlaceFeature=featureasPlaceFeature // Determine if the place ID is for Hana, HI.if(placeFeature.getPlaceId().equals("ChIJ0zQtYiWsVHkRk8lRoB1RNPo")){ // Use FeatureStyle.Builder to configure the FeatureStyle object// returned by the style factory function.return@StyleFactoryFeatureStyle.Builder()// Define a style with purple fill at 50% opacity and// solid purple border..fillColor(0x80810FCB.toInt()).strokeColor(0xFF810FCB.toInt()).build()}}return@StyleFactorynull} // Apply the style factory function to the feature layer.localityLayer?.setFeatureStyle(styleFactory)}
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eDefine a style factory function that implements the \u003ccode\u003eFeatureLayer.StyleFactory\u003c/code\u003e interface to control the styling logic for boundary polygons in a feature layer.\u003c/p\u003e\n"],["\u003cp\u003eApply the style factory to the feature layer using \u003ccode\u003eFeatureLayer.setFeatureStyle()\u003c/code\u003e, customizing stroke and fill colors for polygon borders and interiors.\u003c/p\u003e\n"],["\u003cp\u003eUtilize place IDs to target specific features for styling, retrieving them through Places APIs, Geocoding, or click events.\u003c/p\u003e\n"],["\u003cp\u003eAccess the place ID of a feature using the \u003ccode\u003ePlaceFeature\u003c/code\u003e class within the style factory function to apply location-based styles.\u003c/p\u003e\n"],["\u003cp\u003eRemove styling from a layer by calling \u003ccode\u003eFeatureLayer.setFeatureStyle(null)\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Style a boundary polygon\n\nSelect platform: [Android](/maps/documentation/android-sdk/dds-boundaries/style-polygon \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/dds-boundaries/style-polygon \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/dds-boundaries/style-polygon \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nTo apply styles for stroke and fill to boundary polygons in a feature layer:\n\n1. Create a style factory function that implements the\n [`FeatureLayer.StyleFactory`](/android/reference/com/google/android/gms/maps/model/FeatureLayer.StyleFactory)\n interface. This function defines the styling logic for a feature layer.\n\n2. Call\n [`FeatureLayer.setFeatureStyle()`](/android/reference/com/google/android/gms/maps/model/FeatureLayer#setFeatureStyle(com.google.android.gms.maps.model.FeatureLayer.StyleFactory))\n to apply the style factory function to the feature layer.\n\nThe following example map demonstrates highlighting the boundary polygon for a\nsingle region in a Locality feature layer.\n\nCreate a style factory function\n-------------------------------\n\nThe style factory function is applied to every polygon in the affected feature\nlayer at the time you set the function on the feature layer. This function must\nreturn a [`FeatureStyle`](/android/reference/com/google/android/gms/maps/model/FeatureStyle)\nobject that specifies how to style the polygon.\n| **Note:** If you update or modify the style factory function for a feature layer, you must call `FeatureLayer.setFeatureStyle()` again to reset the function on the feature layer.\n\nMaps SDK for Android passes a\n[`Feature`](/android/reference/com/google/android/gms/maps/model/Feature)\ninstance to the style factory function. The `Feature` instance represents the\nfeature's metadata, giving you access to the metadata in the style factory\nfunction.\n\nThe style factory function should always return consistent results when it is\napplied. For example, if you wanted to randomly color a set of features, the\nrandom part shouldn't take place in the feature style function, as that would\ncause unintended results.\n\nBecause this function runs over every feature in a layer, optimization is\nimportant. To avoid impacting rendering times:\n\n- Enable only the feature layers you need.\n\n- Call `FeatureLayer.setFeatureStyle(null)` when a feature layer is no longer\n in use.\n\n### Set polygon stroke and fill\n\nWhen styling a boundary polygon in the style factory function, you can set the:\n\n- **Stroke color and opacity** of the polygon border in the ARGB color format,\n as defined by the\n [`Color`](https://developer.android.com/reference/android/graphics/Color.html)\n class. The default value is transparent (0x00000000).\n\n- **Stroke width** of the polygon border in screen pixels. The default value\n is 2.\n\n- **Fill color and opacity** of the polygon in the ARGB color format, as\n defined by the\n [`Color`](https://developer.android.com/reference/android/graphics/Color.html)\n class. The default value is transparent (0x00000000).\n\n### Lookup place IDs to target features\n\nMany applications apply styles to a feature based on the feature location. For\nexample, you might want to apply styling to different countries, territories, or\nregions. The feature location is represented by a\n[place ID](/maps/documentation/places/android-sdk/place-id).\n\nPlace IDs uniquely identify a place in the Google Places database and on Google\nMaps. To get a place ID:\n\n- [Use Places APIs and Geocoding](/maps/documentation/android-sdk/dds-boundaries/dds-use-maps-places-apis) to search for regions by name, and get place IDs for regions within specified bounds.\n- [Get data from click events](/maps/documentation/android-sdk/dds-boundaries/handle-events). This returns the Feature corresponding to a region clicked, which provides access to its place ID and feature type category.\n\nCoverage varies by region. See\n[Google boundaries coverage](/maps/documentation/android-sdk/dds-boundaries/coverage)\nfor details.\n\nGeographic names are available from many sources, such as the\n[USGS Board on Geographic Names](https://edits.nationalmap.gov/apps/gaz-domestic/public/search/names),\nand the\n[U.S. Gazetteer Files](https://www.census.gov/geographies/reference-files/time-series/geo/gazetteer-files.html).\n\n### Use PlaceFeature to get a place ID\n\nThe [`PlaceFeature`](/android/reference/com/google/android/gms/maps/model/PlaceFeature)\nclass is a subclass of the `Feature` class.\nIt represents a *place feature* (a feature with a place ID) which includes\nfeatures of type `ADMINISTRATIVE_AREA_LEVEL_1`, `ADMINISTRATIVE_AREA_LEVEL_2`,\n`COUNTRY`, `LOCALITY`, `POSTAL_CODE`, and `SCHOOL_DISTRICT`.\n\nWhen the place ID is available, the\nMaps SDK for Android passes an instance of `PlaceFeature` to the style factory\nfunction so that you can determine the location of the feature.\n\nStyle factory example\n---------------------\n\nThis example applies a style factory function to a polygon in the Locality\nfeature layer. The style factory function determines the place ID of the feature\nby using the `PlaceFeature` instance. If the place ID is for Hana, Hawaii then\nthe function applies a custom fill and stroke style to the polygon:\n\n1. If you haven't already done so, follow the steps in\n [Get Started](/maps/documentation/android/dds-boundaries/start)\n to create a new map ID and map style. Be sure to enable the **Locality**\n feature layer.\n\n2. Get a reference to the Locality feature layer when the map initializes.\n\n ### Java\n\n\n ```java\n private FeatureLayer localityLayer;\n\n @Override\n public void onMapReady(GoogleMap map) {\n // Get the LOCALITY feature layer.\n localityLayer = map.getFeatureLayer(new FeatureLayerOptions.Builder()\n .featureType(FeatureType.LOCALITY)\n .build());\n\n // Apply style factory function to LOCALITY layer.\n styleLocalityLayer();\n }\n ```\n\n \u003cbr /\u003e\n\n ### Kotlin\n\n\n ```java\n private var localityLayer: FeatureLayer? = null\n\n override fun onMapReady(googleMap: GoogleMap) {\n // Get the LOCALITY feature layer.\n localityLayer = googleMap.getFeatureLayer(FeatureLayerOptions.Builder()\n .featureType(FeatureType.LOCALITY)\n .build())\n\n // Apply style factory function to LOCALITY layer.\n styleLocalityLayer()\n }\n ```\n\n \u003cbr /\u003e\n\n3. Create a style factory function and apply it to the Locality\n feature layer.\n\n The following example only applies the function if the place\n ID of the feature is for Hana, Hawaii (\"ChIJ0zQtYiWsVHkRk8lRoB1RNPo\").\n If the specified place ID is not for Hana, Hawaii then the style is not\n applied. \n\n ### Java\n\n\n ```java\n private void styleLocalityLayer() {\n\n // Create the style factory function.\n FeatureLayer.StyleFactory styleFactory = (Feature feature) -\u003e {\n\n // Check if the feature is an instance of PlaceFeature,\n // which contains a place ID.\n if (feature instanceof PlaceFeature) {\n PlaceFeature placeFeature = (PlaceFeature) feature;\n\n // Determine if the place ID is for Hana, HI.\n if (placeFeature.getPlaceId().equals(\"ChIJ0zQtYiWsVHkRk8lRoB1RNPo\")) {\n\n // Use FeatureStyle.Builder to configure the FeatureStyle object\n // returned by the style factory function.\n return new FeatureStyle.Builder()\n // Define a style with purple fill at 50% opacity and solid purple border.\n .fillColor(0x80810FCB)\n .strokeColor(0xFF810FCB)\n .build();\n }\n }\n return null;\n };\n\n // Apply the style factory function to the feature layer.\n localityLayer.setFeatureStyle(styleFactory);\n }\n ```\n\n \u003cbr /\u003e\n\n ### Kotlin\n\n\n ```java\n private fun styleLocalityLayer() {\n\n // Create the style factory function.\n val styleFactory = FeatureLayer.StyleFactory { feature: Feature -\u003e\n\n // Check if the feature is an instance of PlaceFeature,\n // which contains a place ID.\n if (feature is PlaceFeature) {\n val placeFeature: PlaceFeature = feature as PlaceFeature\n\n // Determine if the place ID is for Hana, HI.\n if (placeFeature.getPlaceId().equals(\"ChIJ0zQtYiWsVHkRk8lRoB1RNPo\")) {\n\n // Use FeatureStyle.Builder to configure the FeatureStyle object\n // returned by the style factory function.\n return@StyleFactory FeatureStyle.Builder()\n // Define a style with purple fill at 50% opacity and\n // solid purple border.\n .fillColor(0x80810FCB.toInt())\n .strokeColor(0xFF810FCB.toInt())\n .build()\n }\n }\n return@StyleFactory null\n }\n\n // Apply the style factory function to the feature layer.\n localityLayer?.setFeatureStyle(styleFactory)\n }\n ```\n\n \u003cbr /\u003e\n\nRemove styling from a layer\n---------------------------\n\nTo remove styling from a layer, call `FeatureLayer.setFeatureStyle(null)`."]]