Search code examples
pythondictionaryfolium

Add heatmap to a layer in Folium


I have this sample code:

from glob import glob
import numpy as np
import folium
from folium import plugins
from folium.plugins import HeatMap

lon, lat = -86.276, 30.935 
zoom_start = 5


data = (
    np.random.normal(size=(100, 3)) *
    np.array([[1, 1, 1]]) +
    np.array([[48, 5, 1]])
).tolist()
m = folium.Map([48, 5], tiles='stamentoner', zoom_start=6)

HeatMap(data).add_to(m)
m

enter image description here

How can I add this heat map to a layer so I can hide it if needed?


Solution

  • I would first add your HeatMap to a FeatureGroup and then add that FeatureGroup to the map(m). I would then add a LayerControl to your map (check the upper right corner). Does this suffice?

    from glob import glob
    import numpy as np
    import folium
    from folium import plugins
    from folium.plugins import HeatMap
    
    lon, lat = -86.276, 30.935 
    zoom_start = 5
    
    
    data = (
        np.random.normal(size=(100, 3)) *
        np.array([[1, 1, 1]]) +
        np.array([[48, 5, 1]])
    ).tolist()
    m = folium.Map([48, 5], tiles='stamentoner', zoom_start=6)
    
    HeatMap(data).add_to(folium.FeatureGroup(name='Heat Map').add_to(m))
    folium.LayerControl().add_to(m)
    
    m
    

    enter image description here