4

This is my folium code:

import folium
mp = folium.Map(location=[37, -102], 
                zoom_start=1, 
                tiles="Stamen Terrain", 
                )
display(mp)

This is the output I get: enter image description here

There are two problems with leaflet map:

  1. The continents are displayed 2 times or more in a loop.
  2. The map can be panned endlessly from left to right or vice-versa, in a loop.

Both of these are nuisance. The first issue can be addressed temporarily by setting the zoom_start to something else than 1. But even then, zooming out of the map bring this issue back again. The less said about the second one the better.

Now what I want is to limit the boundary of my map to, say, [-150, 150, -70, 70] or smaller. And I don't want to display beyond this bound, either by panning or zooming. Neither do I want my map to pan infinitely in a loop.

Is it possible to do that in Folium?

SereneWizard
  • 166
  • 3
  • 14
  • https://github.com/python-visualization/folium/issues/648 – Farhan.K Feb 14 '18 at 17:33
  • This is the best solution I've found so far: https://stackoverflow.com/a/42961284/6398434; basically, you need to add a piece of JS in order to set some leaflet parameters limiting the bounds (`setMaxBounds`) and disabling animations (`animate: false`). Other solutions suggest `maxBoundsViscosity`, but it doesn't work as intended since we can still forcibly drag the screen sideways. – JorgeAmVF Jan 13 '20 at 23:37

3 Answers3

6

It's possible! Just use the min_zoom (and max_zoom for the opposite problem) attribute!

f = folium.Figure(width=1000, height=500)
m = folium.Map(location= initial_location, tiles="openstreetmap",
zoom_start=zoom_start_defined, min_zoom = min_zoom_defined).add_to(f)

I think a min_zoom of 2 should do the work

davidvarenne
  • 141
  • 1
  • 2
  • 11
6

One easy way is to use the max_bounds parameter in the Map() function and set it to True. Using this parameter restricts the map to one view of the continents.

Here's an example :

m = folium.Map(location=loc,max_bounds=True)
Cray
  • 2,774
  • 7
  • 22
  • 32
Baumga
  • 61
  • 1
  • 5
3

Thanks for this discussion. I've been trying to get my maps to look better and control better, so this is helpful. For what it's worth,

max_bounds = True

doesn't keep me from zooming out to see multiple versions of the different continents. If I grab the map and move left/right, I can do so, but the view springs back to keep my initial map (say North America) towards the middle of the screen.

f = folium.Figure(width=1000, height=500)
m = folium.Map(location= initial_location,
               tiles="openstreetmap",
               zoom_start=zoom_start_defined, 
               min_zoom = min_zoom_defined
               ).add_to(f)

This works in so far as it keeps the user from zooming out too far and revealing multiple copies of the same continent. But if the user grabs the map and moves left/right, it is possible to scroll to a different version of (say) North America.

But when I combined these two ideas, it did work:

f = folium.Figure(width=1000, height=500)
m = folium.Map(location= initial_location, 
           tiles="openstreetmap",
           zoom_start=zoom_start_defined, 
           min_zoom = min_zoom_defined,
           max_bounds = True 
           ).add_to(f)

This keeps the user from zooming out too far. The user can still grab a the map and scroll left/right to get to another version of (say) North America, but as soon as the mouse button is released, the view springs back to the original version of NA. This isn't as nice as not allowing the user to scroll to the second (or third) version of NA, but since it jumps back, it is an improvement.

Feikname
  • 119
  • 1
  • 6