Python Folium标记聚类颜色自定义

3
我正在使用MarkerCluster在folium中创建一个leaflet地图。我查看了所有文档并搜索了示例,但是我无法弄清楚如何为给定的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)
1个回答

2

不要仅将所有位置信息直接倾入聚类中,您可以循环遍历这些位置信息并为每个位置信息创建一个标记,以便您可以设置标记的颜色。创建完成后,您可以将标记添加到所需的标记聚类中。

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)

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


谢谢!我使用了您的建议;当我意识到branca IFrame元素必须作为Folium弹出窗口传递给Marker时,一切都变得清晰明了:popup = folium.Popup(res_popup)——我一直在尝试直接将branca元素传递给标记器,但弹出表格没有显示。 - mplane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接