I was looking into this repo, where a blue dot is added to your leaflet map, that if you open it on a phone, it follows you as you walk, similar to a google maps option, as shown in this demo.
Usually in R and using Rmd, I would have this code so that I can know where I am on the map:
knitr::opts_chunk$set(echo = TRUE)
library(leaflet)
I usually made this maps for the field
leaflet() %>%
addTiles() %>%
setView(-71.0382679, 42.3489054, zoom = 18) %>%
addEasyButton(easyButton(
icon="fa-crosshairs", title="Locate Me",
onClick=JS("function(btn, map){ map.locate({setView: true, enableHighAccuracy: true }); }")))
That works great for finding your location, but it does not generate a marker where you are, and more importantly, it does not follow you around, you can see an example of that here
So my first try was just to change locate for control.locate, but that did not work.
leaflet() %>%
addTiles() %>%
setView(-71.0382679, 42.3489054, zoom = 18) %>%
addEasyButton(easyButton(
icon="fa-crosshairs", title="Follow Me",
onClick=JS("function(btn, map){ map.control.locate()}")))
I am still thinking of other options, but any help would be welcome, here is the full rmd in github
These GPS features have been implemented in the leaflet.extras package.
Here's a working version based on your MWE
library(leaflet)
library(leaflet.extras)
your_map <- leaflet() %>%
addTiles() %>%
setView(-71.0382679, 42.3489054, zoom = 18) %>%
addControlGPS(
options = gpsOptions(
position = "topleft",
activate = TRUE,
autoCenter = TRUE,
setView = TRUE))
activateGPS(your_map)
The results looks like this:
Spoiler: Chrome thinks I'm in São Paulo right now... (where I'd rather be!)
...and here's a working demo on my Git.
Work's like a charm on my mobile.
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