Skip to content

Problem adding layers by for loop #1857

Open
@SCantergiani

Description

So for some reason if I try to add multiple groups via a for loop, it just keep the last one. Different story if I add them one by one.

This is my code:

mapita = folium.Map(location=[-33.458725289553634, -70.65252746454252], zoom_start=4.4, tiles=None)

fig_layer = []

for i, col in enumerate(color_dict.columns):
    cm = ColorMapLayers(color_dict, col)
    fg = folium.FeatureGroup(col.lower())
    folium.GeoJson(
        data=regiones,  # es el dataframe consolidado (GeoDataFrame)
        name=f"regiones_{col}",
        zoom_on_click=False,  # Hace zoom o no al hacer click en un vector,
        # Como queremos que se vean nuestros vectores
        style_function=lambda feature: {
            "color": "white",
            "weight": 1,
            "Opacity": 0.8,
            "fillColor": "orange",
            "fillOpacity": 0.8,
            "fillColor": cm(color_dict[col][feature["properties"]["CUT_REG"]]),
        },
        # Queremos agregar highligh en hover
        highlight_function=lambda x: {
            "fillColor": "#000000",
            "color": "#000000",
            "fillOpacity": 0.50,
            "weight": 0.1,
        },
        # Hover tooltip
        tooltip=folium.features.GeoJsonTooltip(
            fields=["CUT_REG", "REGION", col],
            aliases=["CUT_REG", "REGION", col],
            style=(
                "background-color: white; color: #333333; font-family: arial; font-size: 8px; padding: 10px;"
            ),
        ),
    ).add_to(fg)

    mapita.add_child(fg)

    fig_layer.append(fg)


GroupedLayerControl(
    groups={"Groups": fig_layer},
    collapsed=False,
).add_to(mapita)

mapita

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions