Una mappa coropletica è un tipo di mappa tematica in cui le aree amministrative sono
colorate o ombreggiate in base a un valore dei dati. Puoi utilizzare una funzione di fabbrica di stili per applicare uno stile a una mappa in base ai dati in cui ogni area amministrativa è associata a un intervallo di valori numerici. La seguente mappa di esempio mostra una
mappa coropletica per gli stati degli Stati Uniti.
In questo esempio, i dati sono costituiti dall'ID luogo dello stato. La funzione
di fabbrica di stile colora in modo condizionale ogni stato in base a un valore hash dell'ID luogo dello stato.
Se non lo hai ancora fatto, segui i passaggi descritti in
Guida introduttiva
per creare un nuovo ID mappa e uno stile di mappa. Assicurati di attivare il
feature layer Livello area amministrativa 1.
Ottieni un riferimento al feature layer Livello area amministrativa 1 quando
la mappa viene inizializzata. Per gli Stati Uniti, questi livelli amministrativi
corrispondono ai singoli stati.
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()}
Crea una funzione di fabbrica di stili e applicala al
feature layer Livello area amministrativa 1. L'esempio seguente applica la funzione al poligono che rappresenta ogni stato degli Stati Uniti.
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)}
[null,null,["Ultimo aggiornamento 2025-08-31 UTC."],[[["\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"]]