When I add a simple map to my shiny application, without adding polygons, the zoom argument works correctly, and the map initializes as it should.
On the other hand, when I add polygons with add_polygon(), the map is initialized more zoomed out than it should.
Why is this happening?
This is my code:
library(mapdeck)
library(sf)
sf = st_as_sf(my_LargeSpatialPolygonDataframe)
output$my_map = renderMapdeck({
mapdeck(token = mytoken, location = c(a, b), zoom = 12,
bearing = -45.00, pitch = 0, style = 'mapbox://styles/mapbox/light-v9') %>%
add_polygon(data = sf,
stroke_colour = "#000000",
stroke_width = 20,
stroke_opacity = 200,
fill_opacity = 0,
layer = "init_polygons")
})
When you add a layer through one of the add_
functions, the layer will calculate the zoom level required to fit the whole of the data in the frame of the window. You can stop this behaviour using update_view = FALSE
Here's a reproducible example
Here the add_polygon()
layer will re-zoom the map to show all the data in the layer. This is the behaviour you're seeing.
library(mapdeck)
set_token("MAPBOX_TOKEN")
mapdeck(
location = c(144.9, -37.8)
, zoom = 11
, bearing = -45.00
, pitch = 0
, style = mapdeck_style("light")
) %>%
add_polygon(
data = spatialwidget::widget_melbourne
, stroke_colour = "#000000"
, stroke_width = 20
, stroke_opacity = 200
, fill_opacity = 0
, layer_id = "init_polygons"
)
This tells the layer to not update the view, so your original location()
values will be used.
mapdeck(
location = c(144.9, -37.8)
, zoom = 11
, bearing = -45.00
, pitch = 0
, style = mapdeck_style("light")
) %>%
add_polygon(
data = spatialwidget::widget_melbourne
, stroke_colour = "#000000"
, stroke_width = 20
, stroke_opacity = 200
, fill_opacity = 0
, layer_id = "init_polygons"
, update_view = F
)