【问题标题】:Python Folium MarkerCluster Color CustomizationPython Folium MarkerCluster 颜色定制
【发布时间】:2018-09-20 15:52:06
【问题描述】:

我正在使用 MarkerCluster 在 folium 中创建传单地图。我一直在阅读文档并搜索示例,但我无法弄清楚如何为给定的 MarkerCluster 或 FeatureGroup 自定义颜色(例如,一组为绿色而不是默认的蓝色)。

我尝试单独创建标记并迭代地将它们添加到 MarkerCluster,这给了我想要的颜色,但是 iFrame html 表格无法正常工作,并且没有出现弹出窗口。

我编写的代码完美无缺(未提供用于弹出窗口的 html 表),但我真的希望能够更改一组标记的颜色并使用我的方法保留弹出窗口代码。任何指导将不胜感激!

or_map = folium.Map(location=OR_COORDINATES, zoom_start=8)

res_popups, res_locations = [], []
com_popups, com_locations = [], []
for idx, row in geo.iterrows():
    if row['Type'] == 'Residential':
        res_locations.append([row['geometry'].y, row['geometry'].x])
        property_type = row['Type']
        property_name = row['Name']
        address = row['address']
        total_units = row['Total Unit']
        iframe = folium.IFrame(table(property_type, property_name, 
                                     address, total_units), width=width, 
                                     height=height)
        res_popups.append(iframe)
    else:
        com_locations.append([row['geometry'].y, row['geometry'].x])
        property_type = row['Type']
        property_name = row['Name']
        address = row['address']
        total_units = row['Total Unit']
        iframe = folium.IFrame(table(property_type, property_name, address, 
                                     total_units), width=width, 
                                     height=height)
        com_popups.append(iframe)


r = folium.FeatureGroup(name='UCPM Residential Properties')
r.add_child(MarkerCluster(locations=res_locations, popups=res_popups))
or_map.add_child(r)

c = folium.FeatureGroup(name='UCPM Commercial Properties')
c.add_child(MarkerCluster(locations=com_locations, popups=com_popups))
or_map.add_child(c)

display(or_map)

【问题讨论】:

    标签: python folium


    【解决方案1】:

    您可以遍历它们并为每个位置创建一个标记,而不是将所有位置转储到集群中 - 这样您就可以设置标记的颜色。创建完成后,您可以将Marker添加到所需的MarkerCluster中。

    for com_location, com_popup in zip(com_locations, com_popups):
        folium.Marker(com_location,
                  popup=com_popup
                  icon=folium.Icon(color='red', icon='info-sign')
                  ).add_to(cluster)
    

    另一种方法是修改样式函数,如here(In[4] 和 In[5])所示。

    【讨论】:

    • 谢谢!我用了你的建议;当我发现必须将 branca IFrame 元素作为 Folium 弹出窗口传递给 Marker 时,这一切都聚集在一起:popup = folium.Popup(res_popup)——我一直试图将 branca 元素直接传递给 Markers 和弹出表没有显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 2020-02-14
    • 2023-01-07
    • 2020-09-16
    相关资源
    最近更新 更多