are*_*ddy 2 google-maps r rgooglemaps ggmap
我和我一起有几个地方的纬度和逻辑信息.这是一个示例:
lat<-c(17.48693,17.49222,17.51965,17.49359,17.49284,17.47077)
long<-c(78.38945,78.39643,78.37835,78.40079,78.40686,78.35874)
Run Code Online (Sandbox Code Playgroud)
我想以某种顺序绘制这些位置(比如上面向量中的第一个元素的lat-long组合将是起点,我需要以相同的顺序行进到最后一个位置),并在R中进行谷歌地图指示.我发现有一个谷歌地图api,我可以从中获取指定位置的谷歌地图截图,在它上面我们需要绘制线条来连接它们.但我需要的是谷歌地图驾驶方向连接位置(而不是ggplot线).请帮忙.
我已经编写了googleway软件包,以使用有效的API密钥访问google maps API.
您可以使用该功能google_directions()获取路线,包括航路点,路线台阶,路段,距离,时间等.
例如
library(googleway)
## using a valid Google Maps API key
key <- "your_api_key"
## Using the first and last coordinates as the origin/destination
origin <- c(17.48693, 78.38945)
destination <- c(17.47077, 78.35874)
## and the coordinates in between as waypoints
waypoints <- list(via = c(17.49222, 78.39643),
via = c(17.51965, 78.37835),
via = c(17.49359, 78.40079),
via = c(17.49284, 78.40686))
## use 'stop' in place of 'via' for stopovers
## get the directions from Google Maps API
res <- google_directions(origin = origin,
destination = destination,
waypoints = waypoints,
key = key) ## include simplify = F to return data as JSON
Run Code Online (Sandbox Code Playgroud)
结果是从Google地图收到的所有数据
## see the structure
# str(res)
Run Code Online (Sandbox Code Playgroud)
您在Google地图上看到的行包含在
res$routes$overview_polyline$points
# [1] "slviBqmm}MSLiA{B^wAj@sB}Ac@...
Run Code Online (Sandbox Code Playgroud)
哪个是编码折线.
要从中获取lat/lon,请使用该函数 decode_pl()
df_polyline <- decode_pl(res$routes$overview_polyline$points)
head(df_polyline)
# lat lon
# 1 17.48698 78.38953
# 2 17.48708 78.38946
# 3 17.48745 78.39008
# 4 17.48729 78.39052
# 5 17.48707 78.39110
# 6 17.48754 78.39128
Run Code Online (Sandbox Code Playgroud)
当然,您可以根据自己的意愿进行绘图
library(leaflet)
leaflet() %>%
addTiles() %>%
addPolylines(data = df_polyline, lat = ~lat, lng = ~lon)
Run Code Online (Sandbox Code Playgroud)
从googleway2.0开始,您可以在Google地图中绘制折线,或者像以前一样使用解码后的坐标,或者直接使用折线
google_map(key = key) %>%
add_polylines(data = data.frame(polyline = res$routes$overview_polyline$points),
polyline = "polyline")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3400 次 |
| 最近记录: |