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

Shapelets数据应用中地图显示但无点位:folium代码排查求助

迈阿密门店位置地图点位不显示问题排查

我正在使用Python和folium库构建一款数据应用,用于制作迈阿密门店位置地图。应用中的地图可正常显示,但所有门店点位均未出现。以下是我的代码:

import folium 
from pandas import DataFrame 
from shapelets.apps import DataApp, FoliumChart

selector = app.selector(title="Select the Postal Code", options=['33122','33125','33126','33127','33128','33129','33130','33131','33132','33133','33134','33135','33136','33137','33138','33139','33140','33141','33142','33143','33144','33145','33146','33147','33148','33149','33150','33151','33152','33153','33155','33156','33157','33161','33162','33165','33166','33167','33168','33169','33170','33172','33173','33174','33175','33176','33177','33178','33179','33180','33181','33182','33183','33184','33185','33186','33187','33188','33189','33190','33193','33194'])

def folium_map(data: DataFrame, selector: int)-> FoliumChart :
    # Folium map
    # create a map of Miami
    l=folium.Map(location = [25.761681
    ,-80.191788], #Initiate map on Miami city
    zoom_start = 10,min_zoom = 10)
    marker_cluster = MarkerCluster().add_to(l)
    data = data[data.postalCode == selector]
    #select other than place on data frame
    not_game_place=data[data["game"] == False]
    #select place that are not gamestop
    not_game=data_game[data_game["game"] == True]
    not_game=not_game[not_game["Gamestop"] == False]
    #select gamestop on data frame
    gamestop=data_game[data_game["Gamestop"] >0]

    #plot the map with all the locations
    for place in not_game_place.iterrows():
        folium.Marker([place["latitude"], place["longitude"]], popup=(place["name"],place["address"])).add_to(marker_cluster)
    #plot the map with all the location
    for place in not_game.iterrows():
        folium.Marker([place["latitude"], place["longitude"]],icon=folium.Icon(color='green', icon='ok-sign'), popup=(place["name"],place["address"])).add_to(marker_cluster)
    #plot the map with all the location
    for place in gamestop.iterrows():
        folium.Marker([place["latitude"], place["longitude"]],icon=folium.Icon(color='red', icon='ok-sign'), popup=(place["name"],place["address"])).add_to(marker_cluster)

    return app.folium_chart(title= "Map of Miami's", folium=l)

# set the map of Miami
l=folium.Map(location = [25.761681
,-80.191788], #Initiate map on Miami city
zoom_start = 10,min_zoom = 10)
# create a button for the folium map
map = app.folium_chart(title= "Map of Miami's", folium=l)
#Bind the map
map.bind(folium_map,data, selector)

app.place(map)

问题排查与修复方案

  • 缺失MarkerCluster导入:代码中使用MarkerCluster()但未导入该模块,需在开头添加:
    from folium.plugins import MarkerCluster
    
  • 未定义data_game变量:代码多处引用data_game但从未初始化,应基于传入的data定义:
    data_game = data[data["game"] == True]
    
  • iterrows()使用错误:iterrows()返回(索引, 行数据)的元组,直接用place["latitude"]会引发KeyError,循环需修改为:
    for idx, place in not_game_place.iterrows():
        folium.Marker([place["latitude"], place["longitude"]], popup=(place["name"],place["address"])).add_to(marker_cluster)
    
    另外两个循环也要做同样修改。
  • 类型不匹配问题:selector是字符串类型(下拉选项为字符串),但函数参数标注为int,且如果data.postalCode是数值类型,会导致筛选无结果。需统一类型,比如将selector转为int:
    data = data[data.postalCode == int(selector)]
    
    或者确保postalCode字段为字符串类型。
  • 数据源未定义:最后绑定的data变量未定义,需确保传入的是正确的门店数据DataFrame。

修正后的示例代码片段

import folium 
from pandas import DataFrame 
from shapelets.apps import DataApp, FoliumChart
from folium.plugins import MarkerCluster  # 新增导入

selector = app.selector(title="Select the Postal Code", options=['33122','33125','33126','33127','33128','33129','33130','33131','33132','33133','33134','33135','33136','33137','33138','33139','33140','33141','33142','33143','33144','33145','33146','33147','33148','33149','33150','33151','33152','33153','33155','33156','33157','33161','33162','33165','33166','33167','33168','33169','33170','33172','33173','33174','33175','33176','33177','33178','33179','33180','33181','33182','33183','33184','33185','33186','33187','33188','33189','33190','33193','33194'])

def folium_map(data: DataFrame, selector: str)-> FoliumChart :  # 修改参数类型为str
    l=folium.Map(location = [25.761681, -80.191788], zoom_start = 10, min_zoom = 10)
    marker_cluster = MarkerCluster().add_to(l)
    # 统一类型筛选
    data = data[data.postalCode == int(selector)]
    
    not_game_place = data[data["game"] == False]
    data_game = data[data["game"] == True]  # 定义data_game
    not_game = data_game[data_game["Gamestop"] == False]
    gamestop = data_game[data_game["Gamestop"] > 0]

    # 修正iterrows循环
    for idx, place in not_game_place.iterrows():
        folium.Marker([place["latitude"], place["longitude"]], popup=f"{place['name']}<br>{place['address']}").add_to(marker_cluster)
    for idx, place in not_game.iterrows():
        folium.Marker([place["latitude"], place["longitude"]], icon=folium.Icon(color='green', icon='ok-sign'), popup=f"{place['name']}<br>{place['address']}").add_to(marker_cluster)
    for idx, place in gamestop.iterrows():
        folium.Marker([place["latitude"], place["longitude"]], icon=folium.Icon(color='red', icon='ok-sign'), popup=f"{place['name']}<br>{place['address']}").add_to(marker_cluster)

    return app.folium_chart(title= "Map of Miami's", folium=l)

# 假设data是已加载的门店数据DataFrame
# data = pd.read_csv("your_store_data.csv")  # 需补充数据源加载逻辑

l=folium.Map(location = [25.761681, -80.191788], zoom_start = 10, min_zoom = 10)
map = app.folium_chart(title= "Map of Miami's", folium=l)
map.bind(folium_map, data, selector)  # 确保data已定义

app.place(map)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 20:44:55