Leaflet R地图的搜索按钮?

4
我在R中使用leaflet库,它是leaflet.js库的一个封装。我想知道是否可能使用R界面(或一些对底层代码的修改)添加查询或搜索按钮?这里有一些针对javascript库的搜索插件:http://leafletjs.com/plugins.html#search--popups,但我不知道如何让它们与R库生成的javascript配合使用。
作为一个最简单的例子,我想在以下地图中添加搜索“location 1”的功能,并显示弹出窗口:
library(leaflet)
df = read.csv(textConnection(
  'Name, Lat, Long
  <b>location 1</b>,42.3401, -71.0589
  <b>location 2</b>,42.3501, -71.0689'))

leaflet(df) %>% 
  addTiles() %>%
  setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
  addMarkers(~Long, ~Lat, popup = ~Name
  )
3个回答

5
使用leafletplugins包添加搜索栏的完整工作示例如下:
devtools::install_github('byzheng/leaflet')
library(leaflet)
library(leafletplugins)

df = read.csv(textConnection(
  'Name, Lat, Long, Name2
  <b>location 1</b>,42.3401, -71.0589, Loc 1
  <b>location 2</b>,42.3501, -71.0689, Loc 2'))

leaflet(df) %>% 
  addTiles() %>%
  setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
  addMarkers(~Long, ~Lat, popup = ~Name, group = 'marker', label = ~Name2) %>%
  addSearchMarker('marker', position='topleft', propertyName = 'label')

5
该功能现在位于leaflet.extras软件包中。 - needRhelp
2
@needRhelp,看起来这个项目并没有得到很好的维护,至少官网没有指向当前维护的分支或其他包。你知道现在有什么可用的吗? - Valentin_Ștefan

3

这个解决方案可行。但是请注意,为了让leafletplugins代码正常工作,您必须使用devtools::install_github('byzheng/leaflet')分叉最新版本的leaflet(而不是通过CRAN获取的版本)。 - Devon

1

查看inlmisc包和AddSearchButton


df = read.csv(textConnection(
'Name, Lat, Long
<b>location 1</b>,42.3401, -71.0589
<b>location 2</b>,42.3501, -71.0689'))

map=leaflet(df) %>% 
addTiles() %>%
setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
addMarkers(~Long, ~Lat, popup = ~Name, group="marker")    

map=inlmisc::AddSearchButton(map, group = "marker", zoom = 15,
                            textPlaceholder = "Search here")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接