如何在Folium中自定义LayerControl?

时间:2018-12-22 19:32:45

标签: python folium

我使用folium.RegularPolygonMarker创建了地图。 但是在LayerControl中,我想将“ macro_element_6a67a2ea0e4b460fb231fd636c605301 ”替换为“ 我的观点”。 此外,我希望默认情况下不选中此复选框。

这是我的代码:

import folium
from folium.plugins import MarkerCluster

points = [[0,0], [10,10], [15,30], [-15,45]]

map=folium.Map(location=[0, 0], zoom_start=4)
marker_cluster = MarkerCluster().add_to(map)
folium.TileLayer('openstreetmap').add_to(map)
folium.TileLayer('Stamen Terrain').add_to(map)
folium.LayerControl().add_to(map)
folium.PolyLine(points, color="black", weight=2.5, opacity=1).add_to(map)

for x in points:
    info = 'test'
    folium.RegularPolygonMarker(location=[x[0], x[1]], popup=info).add_to(marker_cluster)

map.save("Test.html")

enter image description here

1 个答案:

答案 0 :(得分:1)

感谢@Bob Haffner的有用提示。 解决方案是使用FeatureGroup。 这是我的问题的答案:

import folium
from folium.plugins import MarkerCluster

points = [[0,0], [10,10], [15,30], [-15,45]]

map=folium.Map(location=[0, 0], zoom_start=4)
fg=folium.FeatureGroup(name='My Points', show=False)
map.add_child(fg)
marker_cluster = MarkerCluster().add_to(fg)
folium.TileLayer('openstreetmap').add_to(map)
folium.TileLayer('Stamen Terrain').add_to(map)
folium.LayerControl().add_to(map)
folium.PolyLine(points, color="black", weight=2.5, opacity=1).add_to(map)

for x in points:
    info = 'test'
    folium.RegularPolygonMarker(location=[x[0], x[1]], popup=info).add_to(marker_cluster)

map.save("Test.html")

enter image description here