How to customize the LayerControl in Folium?

H. Dave picture H. Dave · Dec 22, 2018 · Viewed 7.1k times · Source

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

Answer

H. Dave picture H. Dave · Dec 23, 2018

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