Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize the LayerControl in Folium?

Tags:

python

folium

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

like image 366
H. Dave Avatar asked Dec 22 '18 19:12

H. Dave


1 Answers

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

like image 139
H. Dave Avatar answered Sep 21 '22 06:09

H. Dave