Folium: Can I display multiple basemaps with folium.LayerControl().add_to()?

Created on 9 Jul 2016  路  3Comments  路  Source: python-visualization/folium

_(first, my apologies if doing it wrong, as this is my first ever github issue, but I have been encouraged by FPAF himself :-)_

In Leaflet there is a way to show multiple basemaps to chose from (as radio buttons) using the code below. I cannot get this to work with folium.LayerControl().add_to(map) because it only takes one parameter (map)

javascript Code:

var landMap = L.tileLayer.grayscale('http://{s}.tile.thunderforest.com/landscape/{z}/{x}/{y}.png', {attribution: 'Map data', maxZoom: 18,  minZoom: 8});
var osmMap = L.tileLayer.grayscale('http://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', 
{attribution: 'Map data', maxZoom: 18,  minZoom: 8});
var baseMaps = {  "Landscape Map": landMap, "OpenStreetMap": osmMap};
L.control.layers(baseMaps, overlayMaps, {collapsed:false}).addTo(map);

Most helpful comment

Sorry, the answer in folium is much simpler than I thought and I spotted it here:
http://deparkes.co.uk/2016/06/10/folium-map-tiles/
It is simply a matter of first defining the map e.g. my_map
then switching on the layer control with
folium.LayerControl().add_to(my_map)
then adding any number of out-of-the-box folium tiles e.g.

folium.TileLayer('openstreetmap').add_to(my_map)
folium.TileLayer('Mapbox Control Room').add_to(my_map)

They will all appear as radio-buttons, with a single one available to be displayed in any given time.

Please close this issue and thanks.

All 3 comments

Sorry, the answer in folium is much simpler than I thought and I spotted it here:
http://deparkes.co.uk/2016/06/10/folium-map-tiles/
It is simply a matter of first defining the map e.g. my_map
then switching on the layer control with
folium.LayerControl().add_to(my_map)
then adding any number of out-of-the-box folium tiles e.g.

folium.TileLayer('openstreetmap').add_to(my_map)
folium.TileLayer('Mapbox Control Room').add_to(my_map)

They will all appear as radio-buttons, with a single one available to be displayed in any given time.

Please close this issue and thanks.

@Seraf69 I got below error when tried!

folium.TileLayer('openstreetmap').add_to(m)

AttributeError: 'module' object has no attribute 'TileLayer'

I believe you need to add the LayerControl after you added TileLayers.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ispmarin picture ispmarin  路  17Comments

sangyh picture sangyh  路  60Comments

nathan3leaf picture nathan3leaf  路  32Comments

AntonioLopardo picture AntonioLopardo  路  15Comments

wangchenwc picture wangchenwc  路  18Comments