نقشه مثال زیر نشان می دهد که چند ضلعی مرزی برای یک منطقه منفرد در یک لایه ویژگی Locality مشخص شده است.
یک عملکرد کارخانه سبک ایجاد کنید
در زمانی که تابع را روی لایه ویژگی تنظیم میکنید، تابع style factory به هر چند ضلعی در لایه ویژگی تحت تأثیر اعمال میشود. این تابع باید یک شی FeatureStyle را برگرداند که نحوه استایل دادن به چند ضلعی را مشخص می کند.
Maps SDK برای Android یک نمونه Feature را به تابع style factory ارسال می کند. نمونه Feature نشان دهنده ابرداده ویژگی است و به شما امکان دسترسی به ابرداده در تابع style factory را می دهد.
تابع style factory همیشه هنگام اعمال باید نتایج ثابتی را نشان دهد. به عنوان مثال، اگر میخواهید مجموعهای از ویژگیها را بهطور تصادفی رنگ کنید، بخش تصادفی نباید در تابع سبک ویژگی قرار بگیرد، زیرا باعث نتایج ناخواسته میشود.
از آنجا که این تابع بر روی هر ویژگی در یک لایه اجرا می شود، بهینه سازی مهم است. برای جلوگیری از تأثیرگذاری بر زمان رندر:
فقط لایه های ویژگی مورد نیاز خود را فعال کنید.
هنگامی که یک لایه ویژگی دیگر استفاده نمی شود، FeatureLayer.setFeatureStyle(null) را فراخوانی کنید.
تنظیم چند ضلعی استروک و پر کنید
هنگام استایل دادن به یک چندضلعی مرزی در تابع style factory، می توانید موارد زیر را تنظیم کنید:
رنگ سکته مغزی و کدورت حاشیه چند ضلعی در قالب رنگ ARGB، همانطور که توسط کلاس Color تعریف شده است. مقدار پیش فرض شفاف است (0x00000000).
عرض خط مرزی چند ضلعی در پیکسل های صفحه نمایش. مقدار پیش فرض 2 است.
رنگ و کدورت چند ضلعی را در قالب رنگ ARGB، همانطور که توسط کلاس Color تعریف شده است، پر کنید. مقدار پیش فرض شفاف است (0x00000000).
شناسه های مکان را برای هدف قرار دادن ویژگی ها جستجو کنید
بسیاری از برنامهها بر اساس مکان ویژگی، سبکهایی را برای یک ویژگی اعمال میکنند. به عنوان مثال، ممکن است بخواهید یک استایل را برای کشورها، مناطق یا مناطق مختلف اعمال کنید. مکان ویژگی با شناسه مکان نشان داده می شود.
شناسههای مکان به طور منحصربهفردی یک مکان را در پایگاه داده Google Places و در Google Maps شناسایی میکنند. برای دریافت شناسه مکان:
دادهها را از رویدادهای کلیک دریافت کنید . این ویژگی مربوط به منطقه ای که کلیک شده است را برمی گرداند، که دسترسی به شناسه مکان و دسته نوع ویژگی آن را فراهم می کند.
از PlaceFeature برای دریافت شناسه مکان استفاده کنید
کلاس PlaceFeature یک زیر کلاس از کلاس Feature است. این یک ویژگی مکان (ویژگی با شناسه مکان) را نشان میدهد که شامل ویژگیهایی از نوع ADMINISTRATIVE_AREA_LEVEL_1 ، ADMINISTRATIVE_AREA_LEVEL_2 ، COUNTRY ، LOCALITY ، POSTAL_CODE ، و SCHOOL_DISTRICT است.
وقتی شناسه مکان در دسترس است، Maps SDK برای Android نمونهای از PlaceFeature را به تابع style factory ارسال میکند تا بتوانید مکان این ویژگی را تعیین کنید.
نمونه کارخانه سبک
این مثال یک تابع style factory را برای چند ضلعی در لایه ویژگی Locality اعمال می کند. تابع style factory شناسه مکان ویژگی را با استفاده از نمونه PlaceFeature تعیین می کند. اگر شناسه مکان برای Hana، Hawaii باشد، این تابع یک سبک پر کردن و ضربهای سفارشی را به چند ضلعی اعمال میکند:
اگر قبلاً این کار را انجام نداده اید، برای ایجاد شناسه نقشه و سبک نقشه جدید، مراحل را در Get Started دنبال کنید. حتما لایه ویژگی Locality را فعال کنید.
هنگامی که نقشه مقداردهی اولیه می شود، یک مرجع به لایه ویژگی Locality دریافت کنید.
جاوا
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();}
کاتلین
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()}
یک تابع style factory ایجاد کنید و آن را در لایه ویژگی Locality اعمال کنید.
مثال زیر فقط در صورتی این تابع را اعمال می کند که شناسه مکان ویژگی برای Hana، هاوایی باشد ("ChIJ0zQtYiWsVHkRk8lRoB1RNPo"). اگر شناسه مکان مشخص شده برای Hana، Hawaii نباشد، این سبک اعمال نمی شود.
جاوا
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);}
کاتلین
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)}
یک ظاهر طراحی شده را از یک لایه حذف کنید
برای حذف استایل از یک لایه، FeatureLayer.setFeatureStyle(null) را فراخوانی کنید.
تاریخ آخرین بهروزرسانی 2025-09-04 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-09-04 بهوقت ساعت هماهنگ جهانی."],[[["\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)`."]]