You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中如何循环GeoJSON属性生成Folium HTML弹窗(解决重复问题)

解决Folium中GeoJSON弹窗内容重复的问题

问题根源

  1. 循环逻辑无效:你在循环里生成了多个popup变量,但最终调用folium.GeoJson时只传入了最后一个popup,导致所有区块共用同一个弹窗内容。
  2. 数据关联错误:popup_html_block函数依赖DataFrame索引获取数据,但GeoJson的feature顺序可能和DataFrame不完全匹配,且无法与每个feature动态绑定。

修复方案

步骤1:修改弹窗HTML生成函数

让函数直接接收GeoJson feature的properties字典,不再依赖DataFrame索引:

def popup_html_block(properties):
    block_name = properties['Block_Name']
    status = properties['Status']
    operator = properties['Operator']
    kilo = properties['Sq. Kilometers']
    miles = properties['Sq. Miles']
    left_col_color = "#65b3d0"
    right_col_color = "#ebf2f7"

    html = f"""<!DOCTYPE html>
    <html>
    <head>
        <h4 style="margin-bottom:10px; width:200px">{block_name}</h4>
    </head>
    <body>
        <table style="height:126px; width:350px;">
            <tbody>
                <tr>
                    <td style="background-color:{left_col_color};">
                        <span style="color:#151515">Status</span>
                    </td>
                    <td style="width:150px; background-color:{right_col_color};">{status}</td>
                </tr>
                <tr>
                    <td style="background-color:{left_col_color};">
                        <span style="color:#151515">Operator</span>
                    </td>
                    <td style="width:150px; background-color:{right_col_color};">{operator}</td>
                </tr>
                <tr>
                    <td style="background-color:{left_col_color};">
                        <span style="color:#151515">Sq. Kilometers</span>
                    </td>
                    <td style="width:150px; background-color:{right_col_color};">{kilo}</td>
                </tr>
                <tr>
                    <td style="background-color:{left_col_color};">
                        <span style="color:#151515">Sq. Miles</span>
                    </td>
                    <td style="width:150px; background-color:{right_col_color};">{miles}</td>
                </tr>
            </tbody>
        </table>
    </body>
    </html>
    """
    return html

步骤2:移除无效循环,动态绑定弹窗

删除原来的for i in range(0, len(All_Blocks))循环,改用folium.GeoJson的popup参数接收lambda函数,为每个feature生成对应弹窗:

# 移除原循环,替换为以下代码
folium.GeoJson(
    data=All_Blocks,
    style_function=lambda feature: {
        'fillColor': '#F1D581' if feature['properties']['Status'] == 'Exploration' else '#65b3d0',
        'color': 'black',
        'weight': 3,
        'fillOpacity': 0.2,
        'dashArray': '5,5'
    },
    highlight_function=lambda x: {'fillOpacity': 1},
    tooltip=folium.features.GeoJsonTooltip(fields=['Block_Name'], aliases=['Name']),
    popup=lambda feature: folium.Popup(
        folium.Html(popup_html_block(feature['properties']), script=True),
        max_width=500
    )
).add_to(map1)

关键修改说明

  1. 函数参数调整:popup_html_block直接使用feature的properties,确保弹窗内容与对应区块严格绑定。
  2. 动态弹窗生成:通过lambda函数传递每个feature给弹窗逻辑,替代无效循环,保证每个区块有独立弹窗。
  3. 修复样式判断:修正了你注释掉的颜色逻辑,现在能根据Status自动切换填充色。

问题截图

弹窗内容重复截图1
弹窗内容重复截图2

内容的提问来源于stack exchange,提问作者naranara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 17:50:37