【问题标题】:How to add routes (polylines) between two markers in leaflet如何在传单中的两个标记之间添加路线(折线)
【发布时间】:2018-06-06 09:50:10
【问题描述】:

我希望在这两点之间添加折线。我该怎么做?

m <- leaflet() %>%
     addPolylines() %>%
     addTiles() %>%  # Add default OpenStreetMap map tiles
     addMarkers(lng = -87.6266382, lat = 41.8674336,
                popup = "starting") %>%
     addMarkers(lng = -87.64847, lat = 41.9168862,
                popup = "Destination")

m  # Print the map

数据示例。

| region      | from_lat   | from_long    | to_lat      | to_long    |
|-------------|------------|------------- |-------------|----------- |
| 1           | 41.8674336 | -87.6266382  | 41.887544   | -87.626487 |
| 2           | 41.8674336 | -87.6266382  | 41.9168862  | -87.64847  |
| 3           | 41.8674336 | -87.6266382  | 41.8190937  | -87.6230967|

【问题讨论】:

  • @jazzurro 是的。我也看到了。但我希望获得像 GPS 一样的路线,但在两个标记之间,而不是显示直线。那可能吗 ?还是我问错了问题?
  • 我想你想检查一下 googleway 包。

标签: r google-maps maps leaflet googleway


【解决方案1】:

@jazzurro 的答案很准确,因此应该仍然是公认的答案。

正如他们所暗示的,如果您愿意并绘制 Google 地图,您可以在 googleway 包中完成所有这些操作。使用传单的主要区别在于您不需要解码折线,谷歌地图可以为您处理编码的字符串。

polylines <- lapply(1:nrow(mydf), function(x){

  foo <- google_directions(origin = unlist(mydf[x, 2:3]),
                           destination = unlist(mydf[x, 4:5]),
                           key = apiKey,
                           mode = "driving",
                           simplify = TRUE)

  ## no need to decode the line, just return the string as-is
  foo$routes$overview_polyline$points
}
)

df <- data.frame(polylines = unlist(polylines), stringsAsFactors = F)

## add some colour values for the markers
mydf$colour_from <- "red"
mydf$colour_to <- "blue"

## plot the polylines and the markers
google_map(key = mapKey) %>%
  add_markers(data = mydf, lat = "from_lat", lon = "from_long", colour = "colour_from") %>%
  add_markers(data = mydf, lat = "to_lat", lon = "to_long", colour = "colour_to") %>%
  add_polylines(data = df, polyline = "polylines")


注意:我是 googleway 作者。

【讨论】:

  • 很高兴您发现了这个问题。我想你会看看是否会有一个 gooleway 标签。 :) 所以我将它添加到问题中。我只是想确认我们需要为每个循环循环 goole_directions() 。那正确吗?或者我们可以一次转储多条路线并避免使用 lapply?
  • @jazzurro - 这是一个很好的问题,我试图找到一种合适的方法来避免循环。但是,由于google_* api 调用一次只能处理一个,我还没有找到解决方法。如果您有任何建议,我很乐意听到。此外,在开发版本中,我一直在添加制作 extracting the results easier 的方法。
  • 如果有这样的限制,我们只需要坚持谷歌所说的。当我在上面编写代码时,我不太确定这一点。很高兴在您的 CRAN 手册或小插图中看到此规则。然后,您的包裹用户会知道他们必须通过一个循环来获取多条路线的数据。在上面的评论中看到他/她的代码,OP 似乎对这一点感到困惑。至于提取结果的方式,这对我来说看起来很不错。 direction_polyline &lt;- function(res) .access_result(resultType(res), "polyline") 是否返回一个带有 long 和 lat 的数据框?
  • @jazzurro - 感谢您的建议;我有added it to the next milestonedirection_polyline 函数只会返回编码的折线,不会对其进行解码。我在my blog 上给出了一个简单的例子。
  • 感谢您对小插图的快速操作。如果您需要任何帮助来修改小插图,请告诉我。我将有一点时间来写一些东西。 access_result() 是从谷歌提取数据的通用函数,对吗?我觉得这很不错。我没有检查您提供的所有信息。如果您没有说明用户可以使用该功能提取哪些类型的信息(例如折线),您可能希望在某处添加该信息。 direction_polyline() 是一个特定的功能,但我认为这很有用。比如我用ggplot2,有时候想画路线
【解决方案2】:

这是我的尝试。由于您想在传单地图上绘制一些路线,因此您希望通过 googleway 包来完成此任务。它允许您使用google_directions()decode_pl() 提取路线数据。由于您有多个路线,因此您想使用lapply() 并创建一个数据集。获得路线数据后,您的工作就很简单了;你使用addPolylines()中的数据。

library(dplyr)
library(googleway)
library(leaflet)

mydf <- data.frame(region = 1:3,
                   from_lat = 41.8674336,
                   from_long = -87.6266382,
                   to_lat = c(41.887544, 41.9168862, 41.8190937),
                   to_long = c(-87.626487, -87.64847, -87.6230967))

mykey <- "you need to have your API key here"

lapply(1:nrow(mydf), function(x){

    foo <- google_directions(origin = unlist(mydf[x, 2:3]),
                             destination = unlist(mydf[x, 4:5]),
                             key = mykey,
                             mode = "driving",
                             simplify = TRUE)

    pl <- decode_pl(foo$routes$overview_polyline$points)

    return(pl)

        }
    ) %>%
bind_rows(.id = "region") -> temp


m <- leaflet() %>%
     addProviderTiles("OpenStreetMap.Mapnik") %>%
     addPolylines(data = temp, lng = ~lon, lat = ~lat, group = ~region) %>%
     addMarkers(lng = -87.6266382, lat = 41.8674336,
                popup = "starting")%>%
     addMarkers(data = mydf, lng = ~to_long, lat = ~to_lat,
                popup = "Destination")

【讨论】:

  • 我可以再问一个问题吗?您提到通过使用 google_directions() 和 decode_pl() ,它可以提取路线数据。我怎么能做到这一点?通过使用 as.numeric(foo) 和 as.numeric(pl).. 对不起,如果我问的是一个愚蠢的问题。我是 R 的新手,尤其是谷歌地图。
  • @EwsonPhang 看看this link
  • @EwsonPhang 看看这个包。您可能能够使用该软件包完成您的任务。包作者在 SO 上。如果你能抓住他/她,你会得到更多的信息。
  • fun_check_location(loc[[x]], type) 中的错误:目的地元素必须是纬度/经度坐标的数值向量或地址字符串
  • df
猜你喜欢
  • 2021-05-14
  • 1970-01-01
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
相关资源
最近更新 更多