PlaceFeature クラスは Feature クラスのサブクラスです。これは、プレイス対象物(プレイス ID をもつ対象物)を表し、ADMINISTRATIVE_AREA_LEVEL_1、ADMINISTRATIVE_AREA_LEVEL_2、COUNTRY、LOCALITY、POSTAL_CODE、および SCHOOL_DISTRICT のタイプの対象物が含まれます。
プレイス ID が利用可能な場合、Maps SDK for Android は 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()}
スタイル ファクトリ関数を作成して、[地域区分] レイヤに適用します。
以下の例では、対象物のプレイス 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,["最終更新日 2024-12-03 UTC。"],[[["\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)`."]]