R leaflet zoomControl option - r

I'm building a map tool in R using leaflet, and I would like to restrict the zoom to a certain area, but the setMaxBounds function doesn't seem to have any effect.
library(dplyr)
library(leaflet)
library(tigris)
ohio_map <- leaflet(counties('OH', cb = TRUE)) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(weight = .3,
color = "#229922",
layerId = ~NAME) %>%
setMaxBounds(lng1 = -84.800,
lat1 = 42.000,
lng2 = -80.500,
lat2 = 38.400)
ohio_map
This shows the right area of the map, but doesn't prevent zooming out.
It would be even better to remove the zoom controls altogether, so that I could replace the navigation with something more suitable to the application at hand. I found a zoomControl option, but haven't been able to figure out where to put that in R to get it to work.
EDIT: As pointed out by #Symbolix, setMaxBounds really is something different than what I'm looking for. I really just want to disable zooming altogether, and remove the controls. The zoomControl option described in the leaflet JavaScript API docs appears to be what I want, but I cannot find that option in the R package.

To remove the zoom controls, set zoomControl = FALSE in leafletOptions. For example:
library(leaflet)
leaflet(options = leafletOptions(zoomControl = FALSE)) %>%
addTiles()
Note that this will not disable zooming via double-clicking or scrolling with your mouse wheel. You can control the zoom level by setting minZoom and maxZoom, again in leafletOptions. To disable zooming, set minZoom equal to maxZoom:
leaflet(options = leafletOptions(zoomControl = FALSE,
minZoom = 3, maxZoom = 3)) %>%
addTiles()
As a bonus, in case you want a "static" view of a map, you can also disable dragging via the dragging option:
leaflet(options = leafletOptions(zoomControl = FALSE,
minZoom = 3, maxZoom = 3,
dragging = FALSE)) %>%
addTiles()
Note that you may need to install the latest github version of leaflet
to implement the above options:
# install github version of leaflet
if (!require('devtools')) install.packages('devtools')
devtools::install_github('rstudio/leaflet')`

Related

Disabling zooming when scrolling over leaflet map in R

i want to disable zooming on my leaflet map in R while having the mouse hover over it.
I tried the suspendScroll(hoverToWake = FALSE) function from the leaflet.extras package as well as leaflet(options = leafletOptions(scrollWheelZoom = FALSE)), but both not working.
leaflet(width = "100%") %>%
setView(0, 0, 1) %>%
addTiles() %>%
suspendScroll(hoverToWake = FALSE)
The map is still zooming in and out when i hover the mouse over it and start scrolling. Am i the only one having this problem? My R Version is 3.6.1
If you want to disable all zooming, you can set the minZoom and maxZoom to the same number as the zoom in setView. Like this reproducible example:
map <- leaflet(options = leafletOptions(minZoom = 10, maxZoom = 10)) %>%
addTiles() %>%
setView(lng = 7.35, lat = 50.05, zoom = 10)
map

Display David Rumsey' Georeferencer tiles in R leaflet

I am trying tu use David Rumsey' Georeferencer tiles in R leaflet package without success (Only the OSM basemap is displayed).
According to the package vignette I tried :
library(leaflet)
# XYZ direct link
leaflet() %>%
setView(0.65, 0, zoom = 5) %>%
addTiles() %>%
addTiles("https://maps.georeferencer.com/georeferences/700526190853/2017-12-30T11:48:27.589686Z/map.json?key=D7AwmpRP1H6pUic6DIK3")
and
library(leaflet)
# WMS tiles
leaflet() %>%
addTiles() %>% setView(0.65, 0, zoom = 5) %>%
addWMSTiles(
"https://maps.georeferencer.com/georeferences/700526190853/2017-12-30T11:48:27.589686Z/wmts?key=D7AwmpRP1H6pUic6DIK3&SERVICE=WMTS&REQUEST=GetCapabilities",
layers = "1", # I assume
options = WMSTileOptions(format = "image/png", transparent = FALSE),
attribution = "")
Notes :
- The access at the map links need free registration.
- I used the 2.1 package release.
- The vignette's addWMSTiles example is working on my computer.
I solve my problem using the addLayersControl function that can allow me to choose the layer to display.
library(leaflet)
m <- leaflet() %>%
addProviderTiles(providers$CartoDB.DarkMatterNoLabels, group = "DarkMatter") %>%
addTiles("https://maps.georeferencer.com/georeferences/700526190853/2017-12-30T11:48:27.589686Z/map/{z}/{x}/{y}.png?key=D7AwmpRP1H6pUic6DIK3", group ="H") %>%
addLayersControl(
baseGroups = c("DarkMatter", "H"), position ="topleft")
m

leaflet.extras: measure distance in metres

I would like to create a map where I can interactively measure the distance between 2 points. Luckily, leaflet.extras has exactly what I need, however, I'm struggling to get it to produce the outputs in metres (or kilometres) as opposed to feet.
Consider the below piece of code:
library(leaflet)
library(leaflet.extras)
leaflet() %>%
addTiles() %>%
addDrawToolbar(
editOptions=editToolbarOptions(selectedPathOptions=selectedPathOptions())
)
It creates the following map:
However, this example (chunk 3) effectively the same code to create the same measuring tool (polyline), except it works in KM, whereas my example works in feet.
If you have any tips that can help me switch to metres as opposed to feet, I would really appreciate it.
The drawPolylineOptions function does not allow to set the option feet=FALSE.
Hence, I suggest to modify drawPolylineOptions as follows:
library(leaflet)
library(leaflet.extras)
mydrawPolylineOptions <- function (allowIntersection = TRUE,
drawError = list(color = "#b00b00", timeout = 2500),
guidelineDistance = 20, metric = TRUE, feet = FALSE, zIndexOffset = 2000,
shapeOptions = drawShapeOptions(fill = FALSE), repeatMode = FALSE) {
leaflet::filterNULL(list(allowIntersection = allowIntersection,
drawError = drawError, guidelineDistance = guidelineDistance,
metric = metric, feet = feet, zIndexOffset = zIndexOffset,
shapeOptions = shapeOptions, repeatMode = repeatMode)) }
leaflet() %>% setView(10.975342,45.421588,9) %>%
addTiles() %>%
addProviderTiles(providers$OpenStreetMap.Mapnik) %>%
addDrawToolbar(
polylineOptions = mydrawPolylineOptions(metric=TRUE, feet=FALSE),
editOptions=editToolbarOptions(selectedPathOptions=selectedPathOptions())
)
Otherwise, using addMeasures you can add to your map a useful tool to measure distances (see the icon at the top right corner of the map).
It is possibile to specify units used to display length results by the primaryLengthUnit option.
leaflet() %>% setView(10.975342,45.421588,9) %>%
addTiles() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addDrawToolbar(
editOptions=editToolbarOptions(selectedPathOptions=selectedPathOptions())
) %>%
addMeasure(primaryLengthUnit="kilometers", secondaryLengthUnit="kilometers")

multiple copies of the world when zooming out in worldmap created with leaflet in R

I am using the leaflet library in R and I am creating a world map with the following code:
leaflet(data = sPDF) %>%
addProviderTiles("Stamen.Watercolor") %>%
addPolygons(fillColor = ~pal(sPDF$Colonizer_col),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = state_popup) %>%
addLegend("bottomright", pal = pal, values = ~na.omit(Colonizer),
title = "Colony Information",
labFormat = labelFormat(prefix = ""),
opacity = 1 ) %>%
addCircles(data=left, lng = ~LONG, lat = ~LAT, weight = 1,
radius = ~sqrt(Totals)*300, popup = ~area_popup_left) %>%
addCircles(data=arrived, lng = ~LONG, lat = ~LAT, weight = 1,
radius = ~sqrt(Totals)*300, popup = ~area_popup_arrive, fillColor = "Green" )%>%
setView(lng = -1.5, lat = 53.4, zoom = 2.5)#%>%# set centre and extent of map
When the map shows up in R it is all fine but when I export it to an .html file it allows the user to zoom out so much that there is three copies of the world map.
I would like to set it so that the maximum zoom out allows only one copy of the map as a webpage (the same way as it is presented in R).
I have tried
tileOptions(maxZoom=5)
but this only affects the zoom while the map is viewed in R, not when it is exported to html.
Leaflet's L.Map class has an option to stop the copying of the map's overlays:
With this option enabled, the map tracks when you pan to another "copy" of the world and seamlessly jumps to the original one so that all overlays like markers and vector layers are still visible.
http://leafletjs.com/reference.html#map-worldcopyjump
The proper way of making sure a user doesn't pan out a certain area is using L.Map's maxBounds option:
When this option is set, the map restricts the view to the given geographical bounds, bouncing the user back when he tries to pan outside the view.
http://leafletjs.com/reference.html#map-maxbounds
In code:
leafletMap(
"map", "100%", "100%",
initialTileLayer = "http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png",
initialTileLayerAttribution = HTML('© OpenStreetMap contributors, © CartoDB'),
options=list(
center = c(0, 0),
zoom = 0,
worldCopyJump = FALSE,
maxBounds = list(
list(-90, -180),
list(90, 180)
)
)
)
You could indeed top that off with setting the noWrap option on your L.TileLayer but all that in fact does is stop the tiles from repeating which is imo not the solution to your actual problem:
If set to true, the tiles just won't load outside the world width (-180 to 180 longitude) instead of repeating.
http://leafletjs.com/reference.html#tilelayer-nowrap

Loading WMS layer in R leaflet using addWMSTiles

I'm trying to add WMS tiles in the R leaflet package - not a problem using this example geoserver WMS:
leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 4) %>% addWMSTiles(
"http://sedac.ciesin.columbia.edu/geoserver/wms",
layers = "energy:energy-pop-exposure-nuclear-plants-locations_plants",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
tileOptions(tms = TRUE),
attribution = "")
However, when I try using WMS from the National Map, I continue to get empty leaflet results despite multiple attempts at trying to set parameters correctly for url and layers:
leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 4) %>%addWMSTiles(
"http://basemap.nationalmap.gov/arcgis/services/USGSHydroNHD/MapServer/WmsServer?",
layers = "0",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
attribution = "")
I've not used leaflet before outside the R leaflet package, so this may be a very novice mistake in setting my parameters in leaflet using this type of WMS
You just need to be zoomed in a little further for the layer to show up. Try this:
leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 7) %>%addWMSTiles(
"http://basemap.nationalmap.gov/arcgis/services/USGSHydroNHD/MapServer/WMSServer?",
layers = "0",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
attribution = "")

Resources