When building a new map with leaflet in R, I'm using setView()
to set the initial map center and zoom level. I understand the function's syntax and with a bunch of trial and error, can get pretty close to what I want. Something like:
library(leaflet); library(magrittr)
leaflet::breweries91 %>%
leaflet() %>%
# see what breweries are close to Ingolstadt
# without putting it or anything else in the center of the frame
setView(11, 49, zoom = 9) %>%
addTiles() %>%
addMarkers()
I came at the setView()
arguments above by zooming the map around to what I liked, then testing coordinates and zoom levels until I got something close to what I arrived at with my mouse.
I'd prefer to be able to set an existing map to the settings I like, then somehow retrieve the current pan/zoom settings from that map and use those settings in my setView()
function.
Is there a way to retrieve those settings from an existing map that I've zoomed/panned? I'm less interested in arriving at the "right" settings programmatically and more interested in bridging the gap between "I clicked around a bit and found something I like" vs. "I coded in some coordinates".
To be clear, I'm sure there's a way to average distances, write a centering function, or determine initial setView()
settings based on the data like this question, but I'm specifically asking about getting map user data, rather than calculating something, mostly because I often don't know what I want to display as the center of the map until I've created an initial version and have explored the data in the map itself.
mapview has a function called addMouseCoordinates
which will render a little strip at the top of your map displaying details about mouse cursor position (incl. zoom). With this you can simply pan/zoom the map to the position you like, put the cursor at the center of the map and use the displayed coordinates and zoom level to construct setView
for your final map.
library(mapview) # loads leaflet automatically
breweries91 %>%
leaflet() %>%
# see what breweries are close to Ingolstadt
# without putting it or anything else in the center of the frame
setView(11, 49, zoom = 9) %>%
addTiles() %>%
addMarkers() %>%
addMouseCoordinates(style = "basic")
# or for the initial scanning simply
mapview(breweries91)