Folium get location and zoom level

Viewed 94

I'd like to find the current view location of a folium map, and the current zoom level.

I created a map object

>> self.map1 = folium.Map(
            tiles='Stamen Terrain',
            zoom_start=zoom0,
            location=coordinate
        )

After a user interacted with it, I'd like to know the current viewed coordinate & zoom level. (in this case to save and recall at the next startup)

>> print(self.map1.getLocation())
>> print(self.map1.getZoom())

yielding

AttributeError: 'Map' object has no attribute 'getLocation'
AttributeError: 'Map' object has no attribute 'getZoom'

I believe folium uses leaflet, which has a getZoom() method itself according to: https://leafletjs.com/reference.html#Map

How can one access these parameters in folium/python?

2 Answers

In folium there is an alternative ways that I know of, to get position by using plugins.

import folium
from folium import plugins
    
m = folium.Map(location = [13.8041258,100.4481389],zoom_start = 12 )

The first method is use MousePosition plugin to get position by mouse position.

roundnum = "function(num) {return L.Util.formatNum(num, 5);};"
plugins.MousePosition(position='topright', separator=' | ', prefix="Position:",lat_formatter=roundnum, lng_formatter=roundnum).add_to(m)
m

The second is use Draw plugin to put a mark on the map and click on it, to see the position.

plugins.Draw(export=False, position='topleft', draw_options=None, edit_options=None).add_to(m)  
m

Unfortunately, I don't know how to get zoom level. Hope this helps

There is no such function in the Folium library. From my own experience I would recommend using the leafletjs library to benefit from more options because I faced the same issues as you in Folium.

Related