如何将 Leaflet.Locate 添加到 R 制作的地图(而不是定位)

How to add Leaflet.Locate to an R made map (instead of locate)

我正在调查这个 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

通常在 R 中并使用 Rmd,我会得到这段代码,这样我就可以知道我在地图上的位置:


title: "测试图" 输出:html_document

knitr::opts_chunk$set(echo = TRUE)

library(leaflet)

旧map.locate选项

我经常为这个领域制作这张地图

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 }); }")))

这对于查找您的位置非常有用,但它不会在您所在的位置生成标记,更重要的是,它不会跟随您四处走动,您可以看到一个例子 here

尝试合并 control.locate

所以我的第一次尝试只是将 locate 更改为 control.locate,但这没有用。

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()}")))

我还在考虑其他选择,但欢迎任何帮助,这里是 full rmd in github

这些 GPS 功能已在 leaflet.extras package 中实现。

这是一个基于您的 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)

结果如下所示:

剧透:Chrome 认为我现在在圣保罗......(我宁愿去那里!)

...here 是我 Git 上的工作演示。

在我的手机上工作就像一个魅力。