How to customize the LayerControl in Folium?

11,202

Thank you to @Bob Haffner for his useful hint. The solution is to use the FeatureGroup. Here the answer to my question:

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

Share:
11,202

Related videos on Youtube

H. Dave
Author by

H. Dave

Updated on June 04, 2022

Comments

  • H. Dave
    H. Dave almost 2 years

    I created a map by using folium.RegularPolygonMarker. But in the LayerControl, I would like to replace "macro_element_6a67a2ea0e4b460fb231fd636c605301" with "My points". Furthermore, I would like the checkbox unchecked by default.

    Here my code:

    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

    • Bob Haffner
      Bob Haffner over 5 years
      You want to create a FeatureGroup with a name and show parameter. Then add your points to that FeatureGroup. Maybe polyline too?