Disabling zooming when scrolling over leaflet map in R - 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

Related

Function for create a zoom limit for don't show Map data not yet available

Is there any way to create a zoom limit function because despite the use of minZoom and maxZoomdoesn't work in:
leaflet() %>%
setView(lng = lng, lat = lat, zoom=17) %>%
addProviderTiles(providers$Esri.WorldImagery, options = m
providerTileOptions(minZoom = 15, maxZoom = 100)) %>%
addLayersControl(options = layersControlOptions(collapsed = TRUE))
for not permit or the setView block the exhibition of satellite images in a zoom that Map data not yet available?
minZoom and maxZoom work fine for me, but in leafet(options = leafletOptions(minZoom = x, maxZoom = y).
Under Initializing Options: https://rstudio.github.io/leaflet/map_widget.html

How to resize large images in R Leaflet marker popups?

My code:
library(leaflet)
df <- as.data.frame(read.csv("arts.csv"))
file <- as.character(df$url)
leaflet() %>% addTiles()
%>% addMarkers(data = df, lng = ~lon, lat = ~lat,
popup = paste0("<img src = ", file, ">"))
%>% popupOptions(maxWidth = "auto")
I used some code snippet from here: Image in R Leaflet marker popups
My question is: how can I easily resize large images appearing in popups?
The illustartion of the problem:
For Example, I would like to make this photo of Cave Rapa Nui smaller to fit the pop-up.
Thank you in advance!
You can adjust the size by using the HTML width attribute.
Edit your code from above to e.g.
map <- leaflet() %>%
addTiles() %>%
addMarkers(data = df,
lng = ~lon,
lat = ~lat,
popup = paste0("<img src = ", file, " width = 300>"))
map # print the map

R leaflet zoomControl option

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')`

Change color of leaflet marker

Is there anyway to change the color of leaflet marker base on the value of some variable. In the following map, for example, I wish to assign marker color based on mag variable:
library(leaflet)
data(quakes)
# Show first 20 rows from the `quakes` dataset
leaflet(data = quakes[1:20,]) %>% addTiles() %>%
addMarkers(~long, ~lat, popup = ~as.character(mag))
I often use the circle markers because you can change both the size and color based on other variables. For example, I have created a binned variable from a continuous using the following code:
# first cut the continuous variable into bins
# these bins are now factors
last$BeatHomeLvl <- cut(last$BeatHome,
c(0,.5,1,2,3,5,100), include.lowest = T,
labels = c('<.5x', '.5-1x', '1-2x', '2-3x', '3-5x','5x+'))
# then assign a palette to this using colorFactor
# in this case it goes from red for the smaller values to yellow and green
# standard stoplight for bad, good, and best
beatCol <- colorFactor(palette = 'RdYlGn', last$BeatHomeLvl)
When you plot it, I use the code for circle markers. The radius/area of the circle is based on the actual value of the factor and then color is assigned according to the bins.
m1 <- leaflet() %>%
addTiles() %>%
addProviderTiles(providers$OpenStreetMap, group = 'Open SM') %>%
addProviderTiles(providers$Stamen.Toner, group = 'Toner') %>%
addProviderTiles(providers$Esri.NatGeoWorldMap, group = 'NG World') %>%
setView(lng = -72, lat = 41, zoom = 8) %>%
addCircleMarkers(data = Jun, lat = ~Lat, lng = ~Lon,
color = ~beatCol(BeatHomeLvl), popup = Jun$Popup,
radius = ~sqrt(BeatHome*50), group = 'Home - Jun') %>%
At the end of your code add a legend. I added some formatting.
addLegend('bottomright', pal = beatCol, values = last$BeatHomeLvl,
title = 'Compare Home<br>Quote Count to<br>3Mos State Avg',
opacity = 1)
This gives you color-coded and sized circles based on a variable and a nice legend.
As far as I know, you need to assign an image file to one level of icon. For instance, if you have three levels in magnitude in the earthquake data, you need to create an icon list with three image paths. Then, you can have three different colors in markers. At least, the following example is getting closer to what you want. I edited a png file and created three png files. You need to specify the paths of the file when you make an icon list.
library(dplyr)
library(leaflet)
mutate(quakes, group = cut(mag, breaks = c(0, 5, 6, Inf), labels = c("blue", "green", "orange"))) -> mydf
### I edit this png file and created my own marker.
### https://raw.githubusercontent.com/lvoogdt/Leaflet.awesome-markers/master/dist/images/markers-soft.png
quakeIcons <- iconList(blue = makeIcon("/Users/jazzurro/Documents/Stack Overflow/blue.png", iconWidth = 24, iconHeight =32),
green = makeIcon("/Users/jazzurro/Documents/Stack Overflow/green.png", iconWidth = 24, iconHeight =32),
orange = makeIcon("/Users/jazzurro/Documents/Stack Overflow/orange.png", iconWidth = 24, iconHeight =32))
leaflet(data = mydf[1:100,]) %>%
addTiles() %>%
addMarkers(icon = ~quakeIcons[group])
This one worked for me:
Source: https://github.com/bhaskarvk/leaflet/blob/master/inst/examples/awesomeMarkers.R
library(leaflet)
icon.glyphicon <- makeAwesomeIcon(icon= 'flag', markerColor = 'blue', iconColor = 'black')
icon.fa <- makeAwesomeIcon(icon = 'flag', markerColor = 'red', library='fa', iconColor = 'black')
icon.ion <- makeAwesomeIcon(icon = 'home', markerColor = 'green', library='ion')
# Marker + Label
leaflet() %>% addTiles() %>%
addAwesomeMarkers(
lng=-118.456554, lat=34.078039,
label='This is a label',
icon = icon.glyphicon)
leaflet() %>% addTiles() %>%
addAwesomeMarkers(
lng=-118.456554, lat=34.078039,
label='This is a label',
icon = icon.fa)
leaflet() %>% addTiles() %>%
addAwesomeMarkers(
lng=-118.456554, lat=34.078039,
label='This is a label',
icon = icon.ion)
# Marker + Static Label using custom label options
leaflet() %>% addTiles() %>%
addAwesomeMarkers(
lng=-118.456554, lat=34.078039,
label='This is a static label',
labelOptions = labelOptions(noHide = T),
icon = icon.fa)
Why not use vector markers based on svg (here is one example implementation - https://github.com/hiasinho/Leaflet.vector-markers) that you can apply any fill color you want to? Instead of having to create a large amount of static image files. Some code involved, yes, but a lot more flexible.
L.Marker uses images (one for the marker, one for the shadow) so that's not possible. You can however use your own images, there's a good write up on the topic among the tutorials on the Leaflet site:
http://leafletjs.com/examples/custom-icons.html

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