Cómo crear un mapa de coropletas

Selecciona la plataforma: Android iOS JavaScript

Un mapa de coropletas es un tipo de mapa temático en el que las áreas administrativas se marcan con distintos colores o sombreados según un valor de datos. Puedes usar una fábrica de estilos para definir el diseño de un mapa según los datos de ubicación de cada área administrativa asociada con un rango de valores numéricos. En el siguiente mapa de ejemplo, se muestra un mapa de coropletas de los estados de Estados Unidos.

En este ejemplo, los datos son el ID de lugar del estado. El estilo Factory colorea condicionalmente cada estado en función de un valor de hash del el ID de lugar del estado.

Captura de pantalla que muestra un mapa de coropletas de los estados de EE.UU.

  1. Si aún no lo has hecho, sigue los pasos en Primeros pasos para crear un nuevo ID y diseño de mapa. Asegúrate de habilitar el Capa del componente de área administrativa nivel 1.

  2. Obtenga una referencia a la capa del componente de área administrativa de nivel 1 cuando la se inicializa el mapa. Para Estados Unidos, estos niveles administrativos se corresponden con los estados individuales.

    Java

    private FeatureLayer areaLevel1Layer;
    @Override public void onMapReady(GoogleMap map) { areaLevel1Layer = map.getFeatureLayer(new FeatureLayerOptions.Builder() .featureType(FeatureType.ADMINISTRATIVE_AREA_LEVEL_1) .build());
    // Apply style factory function to ADMINISTRATIVE_AREA_LEVEL_1 layer. styleAreaLevel1Layer(); }

    Kotlin

    private var areaLevel1Layer: FeatureLayer? = null
    override fun onMapReady(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() }

  3. Crea una función de fábrica de estilo y aplícala al archivo Capa de componentes de nivel 1 del área administrativa. En el siguiente ejemplo se aplica la al polígono que representa cada estado de Estados Unidos.

    Java

    private void styleAreaLevel1Layer() {
      FeatureLayer.StyleFactory styleFactory = (Feature feature) -> {
        if (feature instanceof PlaceFeature) {
          PlaceFeature placeFeature = (PlaceFeature) feature;
    // Return a hueColor in the range [-299,299]. If the value is // negative, add 300 to make the value positive. int hueColor = placeFeature.getPlaceId().hashCode() % 300; if (hueColor < 0) { hueColor += 300; }
    return new FeatureStyle.Builder() // Set the fill color for the state based on the hashed hue color. .fillColor(Color.HSVToColor(150, new float[] {hueColor, 1, 1})) .build(); } return null; };
    // Apply the style factory function to the feature layer. areaLevel1Layer.setFeatureStyle(styleFactory); }

    Kotlin

    private fun styleAreaLevel1Layer() {
      val styleFactory = FeatureLayer.StyleFactory { feature: Feature ->
          if (feature is PlaceFeature) {
              val placeFeature: PlaceFeature = feature as PlaceFeature
    // Return a hueColor in the range [-299,299]. If the value is // negative, add 300 to make the value positive. var hueColor: Int = placeFeature.getPlaceId().hashCode() % 300 if (hueColor < 0) { hueColor += 300 } return@StyleFactory FeatureStyle.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@StyleFactory null }
    // Apply the style factory function to the feature layer. areaLevel1Layer?.setFeatureStyle(styleFactory) }