I have recently discovered the r package “leaflet” and found a great blog with some basic instruction for creating an interactive map (found here) http://mhermans.net/hiking-gpx-r-leaflet.html.
I have not, however been successful at adding a scale bar on the map. That is, I’d like to add a graphic feature that scales distance as you zoom in and out of the map (e.g. a bar at the bottom of the map that represents 1km). The leaflet site (found here) http://leafletjs.com/reference.html#control-scale-l.control.scale discusses this feature: L.control,scale().
Does anyone know how to add a scale bar??
This is the code for my map so far (The “Add Scale Bar” Does NOT work):
# A map of Track data
Mymap <- leaflet() %>% addTiles() %>%
addPolylines(data=Dofu1) %>%
addPolylines(data=Zak1) %>%
addProviderTiles("Esri.WorldImagery")
# Add a legend
Mymap %>%
addLegend(position = 'topright',
colors = "blue",
labels = 'Buruku Tracks', opacity = 0.5,
title = '')
# Add a Scale Bar
Mymap %>%
addControl(Mymap, "Scale",
position = c("topright"),
layerID = NULL,
className = "Scale",
data - getMapData(Mymap))
This feature has been added to the development version of the leaflet package. See Add support for scale bar. Also, the second argument to addControl
expects html as either a character string or html generated from Shiny or htmltools tags. Finally, I think you have a typo in addControl: data - getMapData(Mymap)
should be data = getMapData(Mymap)
the scale bar function is implemented and can be added with 'addScaleBar'
leaflet() %>%
addTiles() %>%
addScaleBar()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With