首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为R中的leaflet添加标签到使用leaftime插件创建的时间线地图

为R中的leaflet添加标签到使用leaftime插件创建的时间线地图
EN

Stack Overflow用户
提问于 2020-01-24 19:10:11
回答 2查看 483关注 0票数 1

下面的示例代码取自R leaftime包的文档中给出的示例。它创建了一张带有时间线的地图,显示了随着时间的推移出现的点。我希望标签被添加到点,以显示每个点的id号。通过查看代码,我假设代码中的JS函数实际上是将JavaScript传递给leaflet以定制地图。我从这里了解到https://gis.stackexchange.com/questions/245621/how-to-label-geojson-points-in-leaflet,我需要添加工具提示函数,但是一直无法修改下面的R代码来达到预期的结果。如果有任何帮助我将不胜感激。

代码语言:javascript
复制
library(leaflet)
library(leaftime)
library(htmltools)

#Build data.frame
power <- data.frame(
"Latitude" = c(
  33.515556, 38.060556, 47.903056, 49.71, 49.041667, 31.934167,
  54.140586, 54.140586, 48.494444, 48.494444
),
"Longitude" = c(
  129.837222, -77.789444, 7.563056, 8.415278, 9.175, -82.343889,
  13.664422, 13.664422, 17.681944, 17.681944
),
"start" = seq.Date(as.Date("2015-01-01"), by = "day", length.out = 10),
"end" = as.Date("2015-01-10")
)
power$id<-seq.int(nrow(power))

# use geojsonio to convert data.frame
power_geo <- geojsonio::geojson_json(power,lat="Latitude",lon="Longitude")

leaflet(power_geo) %>%
addTiles() %>%
setView(44.0665,23.74667,2) %>%
addTimeline(
  timelineOpts = timelineOptions(
    pointToLayer = htmlwidgets::JS(
      "
      function(data, latlng) {
      return L.circleMarker(latlng, {
      radius: 10,
      color: 'black',
      fillColor: 'pink',
      fillOpacity: 1
      })
      }
      "
    )
    )
  )
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-29 22:05:40

扩展来自Falke Design的答案,这里是将id添加到标签的完整代码。

代码语言:javascript
复制
library(leaflet)
library(leaftime)
library(htmltools)

#Build data.frame
power <- data.frame(
"Latitude" = c(
  33.515556, 38.060556, 47.903056, 49.71, 49.041667, 31.934167,
  54.140586, 54.140586, 48.494444, 48.494444
),
"Longitude" = c(
  129.837222, -77.789444, 7.563056, 8.415278, 9.175, -82.343889,
  13.664422, 13.664422, 17.681944, 17.681944
),
"start" = seq.Date(as.Date("2015-01-01"), by = "day", length.out = 10),
"end" = as.Date("2015-01-10")
)
power$id<-seq.int(nrow(power))

# use geojsonio to convert data.frame
power_geo <- geojsonio::geojson_json(power,lat="Latitude",lon="Longitude")

leaflet(power_geo) %>%
  addTiles() %>%
  setView(44.0665,23.74667,2) %>%
  addTimeline(
    timelineOpts = timelineOptions(
      pointToLayer = htmlwidgets::JS(
        "
        function(data, latlng) {
          return L.circleMarker(latlng, {
            radius: 10,
            color: 'black',
            fillColor: 'pink',
            fillOpacity: 1
          }).bindTooltip(
            // data.properties will have the columns from sf in R
            'id: ' + data.properties.id + '<br/>start: ' + data.properties.start,
            {permanent: true}
          ).openTooltip()
        }
        "
      )
    )
  )
票数 1
EN

Stack Overflow用户

发布于 2020-01-24 20:22:09

尝试:

代码语言:javascript
复制
pointToLayer = htmlwidgets::JS(
      "
      function(data, latlng) {
      return L.circleMarker(latlng, {
      radius: 10,
      color: 'black',
      fillColor: 'pink',
      fillOpacity: 1
      }).bindTooltip('I am a circle.',{permanent: true}).openTooltip();
      }
      "
    )

下面是一个使用普通JS的示例:https://jsfiddle.net/falkedesign/yqfs1bnk/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59895273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档