3

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:


title: "Test map" output: html_document

knitr::opts_chunk$set(echo = TRUE)

library(leaflet)

Old map.locate option

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

Trying to incorporate control.locate

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

Derek Corcoran
  • 3,930
  • 2
  • 25
  • 54

1 Answers1

1

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:

Flowers in Chania

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.

Martin C. Arnold
  • 9,483
  • 1
  • 14
  • 22