Sebastian Goslin
Sebastian Goslin

Reputation: 497

Folium Multiple map overlays

I'm fairly new to folium so this might be a bit noobish but I'm currently trying to plot several heatmaps of different data-points and add the ability to switch between the heatmaps all on the same plot. So for example I have such:

# The base map
 hmap = folium.Map(location=[38.908111, -77.008871], tiles="Stamen Terrain", zoom_start=12)

# And each layer

# Homicide 
HeatMap(list(zip(crime_homicide.LATITUDE.values, crime_homicide.LONGITUDE.values))).add_to(folium.FeatureGroup(name='Homicides').add_to(hmap))

# Robbery

HeatMap(list(zip(crime_robbery.LATITUDE.values, crime_robbery.LONGITUDE.values))).add_to(folium.FeatureGroup(name='Robbery').add_to(hmap))

# Assault

HeatMap(list(zip(crime_assault.LATITUDE.values, crime_assault.LONGITUDE.values))).add_to(folium.FeatureGroup(name='Assault').add_to(hmap))

folium.LayerControl(collapsed=False).add_to(hmap)

folium.GeoJson(dc_shape).add_to(hmap)

I tried using folium's FeatureGroup functionality but it looks like thats only specific markers as opposed to whole maps. Is there a way to switch between different maps if they're all heatmaps?

Upvotes: 4

Views: 19025

Answers (1)

DJKarma
DJKarma

Reputation: 182

Your code seems fine. Try this - hmap.add_child

Or you can try heatmapwithtime as well, specifying different metrics which you can adjust in realtime to see different heatmaps.

But,FeatureGroup() will not seem to work with HeatMapWithTime and adding layers directly to the heatmap results in multiple time sliders on the side when there should be only one (common) time slider for all added layers.

So if you want to have a single control you'll have to put all your data in a single geojson and use that.

Why do you add a feature group? If you want to be able to select which instance of HeatMapWithTime you want to display, you can add both add them to the map, and they should both turn up in layer control.

m = Map() HeatMapWithTime(data1).add_to(m) HeatMapWithTime(data2).add_to(m)

FYI, a feature group is meant to group items and display them together. The items themselves don't get added to the map directly. For example:

fg = FeatureGroup().add_to(m) fg.add_child(Item1) fg.add_child(Item2)

Also this is the link, might help you :) https://python-visualization.github.io/folium/plugins.html

Upvotes: 6

Related Questions