jupyter-widgets / ipyleaflet

A Jupyter - Leaflet.js bridge
https://ipyleaflet.readthedocs.io
MIT License
1.49k stars 363 forks source link

Is it possible to select the basemap to display ? #1051

Open 12rambau opened 2 years ago

12rambau commented 2 years ago

IF I create a map with multiple basemaps, only one can be displayed at the time. At first I thought the one displayed is the last added but when using with Voila, the result is random and sometime the first sometimes the second is selected. Is there a way to programatically select the basemap being displayed when the map is first rendered ?

here is a small exemple with 2 basemaps where I would like to see openstreemap selected:

from ipyleaflet import Map, basemaps, LayersControl, TileLayer

# create a second basemap
OpenTopoMap = TileLayer(
    url="https://a.tile.opentopomap.org/{z}/{x}/{y}.png",
    name="OpenTopoMap",
    base=True,
)

map = Map(center=[0,0], zoom=3) 
map.add_layer(OpenTopoMap)
map.add_control(LayersControl())
map
davidbrochart commented 2 years ago

You can use panes with zIndex.

davidbrochart commented 2 years ago

Leaflet also has a bringToFront method. I think it could be nice to expose it in ipyleaflet.

12rambau commented 2 years ago

these 2 tricks are working but they are not changing the value selected in the layer control ? So once it's done if I want to come back to the OpenTopoMap it won't work right?

davidbrochart commented 2 years ago

The user can select the OpenTopoMap from the layer control, it should work. If you want to do it from the back-end, you can probably remove the OpenTopoMap from the map, then add it again with a different pane.