خريطة التوزيعات ذات التظليل المساحي هي نوع من الخرائط الموضوعية التي يتم فيها تلوين أو تظليل المناطق الإدارية وفقًا لقيمة البيانات. يمكنك استخدام دالة مصنع الأنماط لتصميم خريطة استنادًا إلى البيانات التي يرتبط فيها كل منطقة إدارية بنطاق من القيم الرقمية. تعرض خريطة المثال التالية خريطة إحصائية للولايات المتحدة.
في هذا المثال، تتألف البيانات من معرّف المكان الخاص بالولاية. تلوّن دالة إنشاء النمط كل ولاية بشكل مشروط استنادًا إلى قيمة مجزأة لمعرّف المكان الخاص بالولاية.
إذا لم يسبق لك إجراء ذلك، اتّبِع الخطوات الواردة في
البدء
لإنشاء رقم تعريف خريطة ونمط خريطة جديدَين. احرص على تفعيل طبقة الميزات المناطق الإدارية من المستوى 1.
احصل على مرجع لطبقة ميزة "المناطق الإدارية من المستوى 1" عند تهيئة الخريطة. في الولايات المتحدة، تتوافق هذه المستويات الإدارية مع الولايات الفردية.
Java
privateFeatureLayerareaLevel1Layer; @OverridepublicvoidonMapReady(GoogleMapmap){areaLevel1Layer=map.getFeatureLayer(newFeatureLayerOptions.Builder().featureType(FeatureType.ADMINISTRATIVE_AREA_LEVEL_1).build()); // Apply style factory function to ADMINISTRATIVE_AREA_LEVEL_1 layer.styleAreaLevel1Layer();}
Kotlin
privatevarareaLevel1Layer:FeatureLayer?=null overridefunonMapReady(googleMap:GoogleMap){// Get the ADMINISTRATIVE_AREA_LEVEL_1 feature layer.areaLevel1Layer=googleMap.getFeatureLayer(FeatureLayerOptions.Builder().featureType(FeatureType.ADMINISTRATIVE_AREA_LEVEL_1).build()) // Apply style factory function to ADMINISTRATIVE_AREA_LEVEL_1 layer.styleAreaLevel1Layer()}
أنشئ دالة مصنع أنماط وطبِّقها على طبقة المعالم الخاصة بالمناطق الإدارية من المستوى 1. يطبّق المثال التالي الدالة على المضلّع الذي يمثّل كل ولاية من الولايات المتحدة.
Java
privatevoidstyleAreaLevel1Layer(){FeatureLayer.StyleFactorystyleFactory=(Featurefeature)->{if(featureinstanceofPlaceFeature){PlaceFeatureplaceFeature=(PlaceFeature)feature; // Return a hueColor in the range [-299,299]. If the value is// negative, add 300 to make the value positive.inthueColor=placeFeature.getPlaceId().hashCode()%300;if(hueColor < 0){hueColor+=300;} returnnewFeatureStyle.Builder()// Set the fill color for the state based on the hashed hue color..fillColor(Color.HSVToColor(150,newfloat[]{hueColor,1,1})).build();}returnnull;}; // Apply the style factory function to the feature layer.areaLevel1Layer.setFeatureStyle(styleFactory);}
Kotlin
privatefunstyleAreaLevel1Layer(){valstyleFactory=FeatureLayer.StyleFactory{feature:Feature->
if(featureisPlaceFeature){valplaceFeature:PlaceFeature=featureasPlaceFeature // Return a hueColor in the range [-299,299]. If the value is// negative, add 300 to make the value positive.varhueColor:Int=placeFeature.getPlaceId().hashCode()%300if(hueColor < 0){hueColor+=300}return@StyleFactoryFeatureStyle.Builder()// Set the fill color for the state based on the hashed hue color..fillColor(Color.HSVToColor(150,floatArrayOf(hueColor.toFloat(),1f,1f))).build()}return@StyleFactorynull} // Apply the style factory function to the feature layer.areaLevel1Layer?.setFeatureStyle(styleFactory)}
تاريخ التعديل الأخير: 2025-08-31 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-08-31 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eChoropleth maps use color variations to represent data values associated with geographic areas, like shading states based on population.\u003c/p\u003e\n"],["\u003cp\u003eThis guide explains how to create a choropleth map using a style factory function that colors administrative areas based on data ranges.\u003c/p\u003e\n"],["\u003cp\u003eYou'll learn to enable the "Administrative Area Level 1" feature, access this layer, and apply a style factory function to it, demonstrated with code snippets for Android (Java/Kotlin).\u003c/p\u003e\n"],["\u003cp\u003eThe example provided utilizes a hashed value of the place ID to determine the color of each state, although real-world scenarios would likely involve a lookup table for color assignments.\u003c/p\u003e\n"]]],[],null,["# Make a choropleth map\n\nSelect platform: [Android](/maps/documentation/android-sdk/dds-boundaries/choropleth-map \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/dds-boundaries/choropleth-map \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/dds-boundaries/choropleth-map \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nA choropleth map is a type of thematic map in which administrative areas are\ncolored or shaded according to a data value. You can use a style factory\nfunction to style a map based on data where each administrative area is\nassociated with a range of numeric values. The following example map shows a\nchoropleth map for the states of the United States.\n\nIn this example, the data consists of the place ID of the state. The style\nfactory function conditionally colors each state based on a hashed value of the\nplace ID for the state.\n\n| **Note:** Typically, you would define a table of the color for each state, and then lookup the color for each state based on the state's place ID.\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\n **Administrative Area Level 1** feature layer.\n\n2. Get a reference to the Administrative Area Level 1 feature layer when the\n map initializes. For the United States, these administrative levels\n correspond to the individual states.\n\n ### Java\n\n\n ```java\n private FeatureLayer areaLevel1Layer;\n\n @Override\n public void onMapReady(GoogleMap map) {\n areaLevel1Layer = map.getFeatureLayer(new FeatureLayerOptions.Builder()\n .featureType(FeatureType.ADMINISTRATIVE_AREA_LEVEL_1)\n .build());\n\n // Apply style factory function to ADMINISTRATIVE_AREA_LEVEL_1 layer.\n styleAreaLevel1Layer();\n }\n ```\n\n \u003cbr /\u003e\n\n ### Kotlin\n\n\n ```java\n private var areaLevel1Layer: FeatureLayer? = null\n\n override fun onMapReady(googleMap: GoogleMap) {\n // Get the ADMINISTRATIVE_AREA_LEVEL_1 feature layer.\n areaLevel1Layer = googleMap.getFeatureLayer(FeatureLayerOptions.Builder()\n .featureType(FeatureType.ADMINISTRATIVE_AREA_LEVEL_1)\n .build())\n\n // Apply style factory function to ADMINISTRATIVE_AREA_LEVEL_1 layer.\n styleAreaLevel1Layer()\n }\n ```\n\n \u003cbr /\u003e\n\n3. Create a style factory function and apply it to the\n Administrative Area Level 1 feature layer. The following example applies the\n function to the polygon representing each state of the United States.\n\n ### Java\n\n\n ```java\n private void styleAreaLevel1Layer() {\n FeatureLayer.StyleFactory styleFactory = (Feature feature) -\u003e {\n if (feature instanceof PlaceFeature) {\n PlaceFeature placeFeature = (PlaceFeature) feature;\n\n // Return a hueColor in the range [-299,299]. If the value is\n // negative, add 300 to make the value positive.\n int hueColor = placeFeature.getPlaceId().hashCode() % 300;\n if (hueColor \u003c 0) {\n hueColor += 300;\n }\n\n return new FeatureStyle.Builder()\n // Set the fill color for the state based on the hashed hue color.\n .fillColor(Color.HSVToColor(150, new float[] {hueColor, 1, 1}))\n .build();\n }\n return null;\n };\n\n // Apply the style factory function to the feature layer.\n areaLevel1Layer.setFeatureStyle(styleFactory);\n }\n ```\n\n \u003cbr /\u003e\n\n ### Kotlin\n\n\n ```java\n private fun styleAreaLevel1Layer() {\n val styleFactory = FeatureLayer.StyleFactory { feature: Feature -\u003e\n if (feature is PlaceFeature) {\n val placeFeature: PlaceFeature = feature as PlaceFeature\n\n // Return a hueColor in the range [-299,299]. If the value is\n // negative, add 300 to make the value positive.\n var hueColor: Int = placeFeature.getPlaceId().hashCode() % 300\n if (hueColor \u003c 0) {\n hueColor += 300\n }\n return@StyleFactory FeatureStyle.Builder()\n // Set the fill color for the state based on the hashed hue color.\n .fillColor(Color.HSVToColor(150, floatArrayOf(hueColor.toFloat(), 1f, 1f)))\n .build()\n }\n return@StyleFactory null\n }\n\n // Apply the style factory function to the feature layer.\n areaLevel1Layer?.setFeatureStyle(styleFactory)\n }\n ```\n\n \u003cbr /\u003e"]]