توضّح خريطة المثال التالية كيفية تمييز المضلّع الحدودي لمنطقة واحدة في طبقة معالم "المنطقة المحلية".
إنشاء دالة مصنع أنماط
يتم تطبيق دالة إنشاء النمط على كل مضلّع في طبقة المعالم المتأثرة عند ضبط الدالة على طبقة المعالم. يجب أن تعرض هذه الدالة كائن FeatureStyle يحدّد طريقة تصميم المضلّع.
تمرِّر حزمة تطوير البرامج بالاستناد إلى بيانات "خرائط Google" لتطبيقات Android مثيلاً من
Feature إلى دالة إنشاء الأنماط. يمثّل عنصر Feature البيانات الوصفية للميزة، ما يتيح لك الوصول إلى البيانات الوصفية في دالة مصنع الأنماط.
يجب أن تعرض دالة إنشاء الأنماط دائمًا نتائج متسقة عند تطبيقها. على سبيل المثال، إذا أردت تلوين مجموعة من العناصر بشكل عشوائي، يجب ألا يتم الجزء العشوائي في دالة نمط العنصر، لأنّ ذلك سيؤدي إلى نتائج غير مقصودة.
بما أنّ هذه الدالة يتم تشغيلها على كل عنصر في الطبقة، فإنّ التحسين مهم. لتجنُّب التأثير في أوقات العرض:
فعِّل طبقات الميزات التي تحتاج إليها فقط.
استدعِ FeatureLayer.setFeatureStyle(null) عندما لا تكون طبقة المعالم قيد الاستخدام.
ضبط خط المضلّع وتعبئته
عند تصميم مضلّع حدودي في دالة إنشاء الأنماط، يمكنك ضبط ما يلي:
لون الخط ودرجة تعتيمه لحدود المضلّع بتنسيق لون ARGB،
كما هو محدّد بواسطة
فئة Color. القيمة التلقائية هي قيمة شفافة (0x00000000).
عرض الخط لحدود المضلّع بوحدات البكسل على الشاشة القيمة التلقائية هي 2.
لون التعبئة ودرجة التعتيم للمضلّع بتنسيق لون ARGB، كما هو محدّد بواسطة الفئة Color. القيمة التلقائية هي قيمة شفافة (0x00000000).
البحث عن أرقام تعريف الأماكن لاستهداف الميزات
تطبّق العديد من التطبيقات أنماطًا على عنصر استنادًا إلى موقعه الجغرافي. على سبيل المثال، قد تريد تطبيق نمط على بلدان أو مناطق أو أقاليم مختلفة. يتم تمثيل الموقع الجغرافي للميزة من خلال
معرّف مكان.
تحدِّد أرقام تعريف الأماكن مكانًا بشكلٍ فريد في قاعدة بيانات أماكن Google وعلى "خرائط Google". للحصول على معرّف مكان، اتّبِع الخطوات التالية:
فئة PlaceFeature هي فئة فرعية من فئة Feature.
يمثّل هذا النوع ميزة مكان (ميزة تتضمّن رقم تعريف مكان) تشمل ميزات من النوع ADMINISTRATIVE_AREA_LEVEL_1 وADMINISTRATIVE_AREA_LEVEL_2 وCOUNTRY وLOCALITY وPOSTAL_CODE وSCHOOL_DISTRICT.
عندما يتوفّر معرّف المكان، تمرِّر حزمة تطوير البرامج (SDK) لخرائط Google على Android مثيلاً من PlaceFeature إلى دالة مصنع الأنماط
لتتمكّن من تحديد الموقع الجغرافي للعنصر.
مثال على مصنع الأنماط
يطبّق هذا المثال دالة مصنع أنماط على مضلّع في طبقة ميزات "الموقع الجغرافي". تحدّد دالة إنشاء الأنماط رقم تعريف المكان الخاص بالعنصر
باستخدام مثيل PlaceFeature. إذا كان معرّف المكان يخصّ مدينة هانا في هاواي، ستطبّق الدالة نمطًا مخصّصًا للتعبئة والحدود على المضلّع:
إذا لم يسبق لك إجراء ذلك، اتّبِع الخطوات الواردة في
البدء
لإنشاء رقم تعريف خريطة ونمط خريطة جديدَين. تأكَّد من تفعيل طبقة المعالم المحلية.
احصل على مرجع لطبقة ميزات "الموقع الجغرافي" عند تهيئة الخريطة.
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.
لا يطبّق المثال التالي الدالة إلا إذا كان معرّف المكان الخاص بالعنصر الجغرافي هو Hana، هاواي ("ChIJ0zQtYiWsVHkRk8lRoB1RNPo").
إذا كان رقم تعريف المكان المحدّد لا يخصّ مدينة هانا في هاواي، لن يتم تطبيق النمط.
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)}
إزالة الأنماط من طبقة
لإزالة الأنماط من طبقة، استخدِم FeatureLayer.setFeatureStyle(null).
تاريخ التعديل الأخير: 2025-09-05 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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-05 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\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)`."]]