ggmap 路线查找 - 不会停留在道路上
ggmap route finding - doesn't stay on roads
我正在尝试使用 ggmap 中的 route() 函数映射路线。我的问题是路线不会停留在路上。我的 route_df <- route(origin, destination, structure = "route")
代码是否遗漏了什么?或者是否有可用于完成此操作的替代函数?
示例代码:
install_github("rstudio/leaflet")
library(ggmap)
library(leaflet)
way1txt <- "Tinsletown, Vancouver, BC"
way2txt <- "Science World, Vancouver, BC"
route_df <- route(way1txt, way2txt, structure = "route")
# Map using Leaflet R
m = leaflet() %>% addTiles()
m = m %>% addPolylines(route_df$lon, route_df$lat, fill = FALSE)
m = m %>% addPopups(route_df$lon[1], route_df$lat[1], 'Origin')
m = m %>% addPopups(route_df$lon[length(route_df$lon)],
route_df$lat[length(route_df$lon)], 'Destination')
m
地图截图:
由于默认 output=
参数设置为 simple
,因此您实际上并未从 route()
获取所需的多段线。您可能需要如下所示将其更改为 all
并开始解码折线。
下面是一种基于取自 here 的 decodeLine()
函数的解决方案。他们的解决方案是定义一个自定义函数来解码折线,然后绘制它解码的所有内容。
library(ggmap)
library(leaflet)
way1txt <- "Tinsletown, Vancouver, BC"
way2txt <- "Science World, Vancouver, BC"
route_all <- route(way1txt, way2txt, structure = "route",
output = "all")
# Custom decode function
# Taken from http://s4rdd.blogspot.com/2012/12/google-maps-api-decoding-polylines-for.html
decodeLine <- function(encoded){
require(bitops)
vlen <- nchar(encoded)
vindex <- 0
varray <- NULL
vlat <- 0
vlng <- 0
while(vindex < vlen){
vb <- NULL
vshift <- 0
vresult <- 0
repeat{
if(vindex + 1 <= vlen){
vindex <- vindex + 1
vb <- as.integer(charToRaw(substr(encoded, vindex, vindex))) - 63
}
vresult <- bitOr(vresult, bitShiftL(bitAnd(vb, 31), vshift))
vshift <- vshift + 5
if(vb < 32) break
}
dlat <- ifelse(
bitAnd(vresult, 1)
, -(bitShiftR(vresult, 1)+1)
, bitShiftR(vresult, 1)
)
vlat <- vlat + dlat
vshift <- 0
vresult <- 0
repeat{
if(vindex + 1 <= vlen) {
vindex <- vindex+1
vb <- as.integer(charToRaw(substr(encoded, vindex, vindex))) - 63
}
vresult <- bitOr(vresult, bitShiftL(bitAnd(vb, 31), vshift))
vshift <- vshift + 5
if(vb < 32) break
}
dlng <- ifelse(
bitAnd(vresult, 1)
, -(bitShiftR(vresult, 1)+1)
, bitShiftR(vresult, 1)
)
vlng <- vlng + dlng
varray <- rbind(varray, c(vlat * 1e-5, vlng * 1e-5))
}
coords <- data.frame(varray)
names(coords) <- c("lat", "lon")
coords
}
route_df <- decodeLine( route_all$routes[[1]]$overview_polyline$points )
# Map using Leaflet R
m = leaflet() %>% addTiles()
m = m %>% addPolylines(route_df$lon, route_df$lat, fill = FALSE)
m = m %>% addPopups(route_df$lon[1], route_df$lat[1], 'Origin')
m = m %>% addPopups(route_df$lon[length(route_df$lon)],
route_df$lat[length(route_df$lon)], 'Destination')
m
我明白了:
供参考,@diegovalle 编写了另一个 decodeLine
函数 here。
你可以使用我的 googleway
包
- 获取路线
- 解码折线(如果需要)
- 在 Google 地图上绘制路线,作为编码折线或解码点
library(googleway)
apiKey <- 'your_api_key'
mapKey <- 'your_maps_api_key'
res <- google_directions(origin = "Tinsletown, Vancouver, BC",
destination = "Science World, Vancouver, BC",
key = apiKey)
折线在res$routes$overview_polyline$points
如果你愿意,你可以解码折线
pl <- res$routes$overview_polyline$points
decode_pl(pl)
# lat lon
# 1 49.28025 -123.1076
# 2 49.27969 -123.1076
# 3 49.27823 -123.1076
# 4 49.27711 -123.1077
# 5 49.27707 -123.1043
但你不必,你可以直接画线
df <- data.frame(polyline = pl)
google_map(key = mapKey, search_box = T) %>%
add_polylines(data = df, polyline = "polyline")
备注
- 问题提出后原点似乎不存在或已移动
我正在尝试使用 ggmap 中的 route() 函数映射路线。我的问题是路线不会停留在路上。我的 route_df <- route(origin, destination, structure = "route")
代码是否遗漏了什么?或者是否有可用于完成此操作的替代函数?
示例代码:
install_github("rstudio/leaflet")
library(ggmap)
library(leaflet)
way1txt <- "Tinsletown, Vancouver, BC"
way2txt <- "Science World, Vancouver, BC"
route_df <- route(way1txt, way2txt, structure = "route")
# Map using Leaflet R
m = leaflet() %>% addTiles()
m = m %>% addPolylines(route_df$lon, route_df$lat, fill = FALSE)
m = m %>% addPopups(route_df$lon[1], route_df$lat[1], 'Origin')
m = m %>% addPopups(route_df$lon[length(route_df$lon)],
route_df$lat[length(route_df$lon)], 'Destination')
m
地图截图:
由于默认 output=
参数设置为 simple
,因此您实际上并未从 route()
获取所需的多段线。您可能需要如下所示将其更改为 all
并开始解码折线。
下面是一种基于取自 here 的 decodeLine()
函数的解决方案。他们的解决方案是定义一个自定义函数来解码折线,然后绘制它解码的所有内容。
library(ggmap)
library(leaflet)
way1txt <- "Tinsletown, Vancouver, BC"
way2txt <- "Science World, Vancouver, BC"
route_all <- route(way1txt, way2txt, structure = "route",
output = "all")
# Custom decode function
# Taken from http://s4rdd.blogspot.com/2012/12/google-maps-api-decoding-polylines-for.html
decodeLine <- function(encoded){
require(bitops)
vlen <- nchar(encoded)
vindex <- 0
varray <- NULL
vlat <- 0
vlng <- 0
while(vindex < vlen){
vb <- NULL
vshift <- 0
vresult <- 0
repeat{
if(vindex + 1 <= vlen){
vindex <- vindex + 1
vb <- as.integer(charToRaw(substr(encoded, vindex, vindex))) - 63
}
vresult <- bitOr(vresult, bitShiftL(bitAnd(vb, 31), vshift))
vshift <- vshift + 5
if(vb < 32) break
}
dlat <- ifelse(
bitAnd(vresult, 1)
, -(bitShiftR(vresult, 1)+1)
, bitShiftR(vresult, 1)
)
vlat <- vlat + dlat
vshift <- 0
vresult <- 0
repeat{
if(vindex + 1 <= vlen) {
vindex <- vindex+1
vb <- as.integer(charToRaw(substr(encoded, vindex, vindex))) - 63
}
vresult <- bitOr(vresult, bitShiftL(bitAnd(vb, 31), vshift))
vshift <- vshift + 5
if(vb < 32) break
}
dlng <- ifelse(
bitAnd(vresult, 1)
, -(bitShiftR(vresult, 1)+1)
, bitShiftR(vresult, 1)
)
vlng <- vlng + dlng
varray <- rbind(varray, c(vlat * 1e-5, vlng * 1e-5))
}
coords <- data.frame(varray)
names(coords) <- c("lat", "lon")
coords
}
route_df <- decodeLine( route_all$routes[[1]]$overview_polyline$points )
# Map using Leaflet R
m = leaflet() %>% addTiles()
m = m %>% addPolylines(route_df$lon, route_df$lat, fill = FALSE)
m = m %>% addPopups(route_df$lon[1], route_df$lat[1], 'Origin')
m = m %>% addPopups(route_df$lon[length(route_df$lon)],
route_df$lat[length(route_df$lon)], 'Destination')
m
我明白了:
供参考,@diegovalle 编写了另一个 decodeLine
函数 here。
你可以使用我的 googleway
包
- 获取路线
- 解码折线(如果需要)
- 在 Google 地图上绘制路线,作为编码折线或解码点
library(googleway)
apiKey <- 'your_api_key'
mapKey <- 'your_maps_api_key'
res <- google_directions(origin = "Tinsletown, Vancouver, BC",
destination = "Science World, Vancouver, BC",
key = apiKey)
折线在res$routes$overview_polyline$points
如果你愿意,你可以解码折线
pl <- res$routes$overview_polyline$points
decode_pl(pl)
# lat lon
# 1 49.28025 -123.1076
# 2 49.27969 -123.1076
# 3 49.27823 -123.1076
# 4 49.27711 -123.1077
# 5 49.27707 -123.1043
但你不必,你可以直接画线
df <- data.frame(polyline = pl)
google_map(key = mapKey, search_box = T) %>%
add_polylines(data = df, polyline = "polyline")
备注
- 问题提出后原点似乎不存在或已移动