【问题标题】:Search button for Leaflet R map?Leaflet R 地图的搜索按钮?
【发布时间】:2019-05-13 21:16:08
【问题描述】:

我在 R 中使用 Leaflet 库,它是 Leaflet.js 库的包装器。我想知道是否可以使用 R 界面添加查询或搜索按钮(或对底层代码进行一些破解)? http://leafletjs.com/plugins.html#search--popups 这里有 javascript 库的搜索插件,但我不知道如何让它们与来自 R 库的 javascript 一起工作。

作为一个最小的例子,我想在下面的地图中添加搜索“位置 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
  )

【问题讨论】:

    标签: javascript r leaflet


    【解决方案1】:

    使用 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')
    

    【讨论】:

    • 该功能现在位于 leaflet.extras 包中。
    • @needRhelp,似乎这个项目不再真正维护了,或者至少登陆页面没有指向当前维护的 fork 或其他包。你碰巧知道现在什么有效吗?
    【解决方案2】:

    R 传单包好像有个搜索插件:https://github.com/byzheng/leafletplugins

    【讨论】:

    • 此解决方案有效。不过,请注意,要让leafletplugins 代码正常工作,您必须使用devtools::install_github('byzheng/leaflet') fork 最新版本的leaflet(不是通过CRAN 发布的)
    【解决方案3】:

    Check outinlmisc 包和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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-13
      • 1970-01-01
      • 2013-12-22
      • 2016-02-16
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 2013-11-25
      相关资源
      最近更新 更多