Um mapa coroplético é um tipo temático em que áreas administrativas ficam coloridas ou sombreadas de acordo com um valor de dados. Você pode usar uma função de fábrica de estilos para estilizar um mapa com base em dados em que cada área administrativa está associada a um intervalo de valores numéricos. O exemplo abaixo mostra um mapa coroplético dos estados dos Estados Unidos.
Neste exemplo, os dados consistem no ID do lugar do estado. A função de fábrica de estilo colore condicionalmente cada estado com base em um valor hash do ID do lugar do estado.
Consulte Começar para criar um novo ID e estilo de mapa, caso ainda não tenha feito isso. Ative a camada de elemento Área administrativa de nível 1.
Receba uma referência à camada de elemento de área administrativa nível 1 quando o mapa for inicializado. Nos Estados Unidos, esses níveis administrativos correspondem aos estados.
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()}
Crie uma função de fábrica de estilos e aplique-a à camada de elementos "Área político-administrativa de nível 1". O exemplo a seguir aplica a função ao polígono que representa cada estado dos Estados Unidos.
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)}
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Não contém as informações de que eu preciso","missingTheInformationINeed","thumb-down"],["Muito complicado / etapas demais","tooComplicatedTooManySteps","thumb-down"],["Desatualizado","outOfDate","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Problema com as amostras / o código","samplesCodeIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-08-16 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,["Select 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"]]