【问题标题】:How to display google map directions given latitude and longitude information in r如何在r中显示给定纬度和经度信息的谷歌地图方向
【发布时间】:2016-01-29 17:14:41
【问题描述】:

我有几个地点的纬度和经度信息。这是一个示例:

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)

我想用 R 中的谷歌地图方向以某种顺序绘制这些位置(比如上述向量中第一个元素的经纬度组合将是起点,我需要以相同的顺序行驶到最后一个位置)。经过一番搜索,我发现有一个谷歌地图 api,我可以从中获取指定位置的谷歌地图截图,在它之上,我们需要绘制线来连接它们。但我需要的是谷歌地图行车路线来连接位置(不是 ggplot 线)。 请帮忙。

【问题讨论】:

    标签: r google-maps ggmap rgooglemaps


    【解决方案1】:

    我已经编写了包 googleway 以使用有效的 API 密钥访问谷歌地图 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
    

    结果是从谷歌地图接收到的所有数据

    ## see the structure
    # str(res)
    

    您在 Google 地图上看到的线包含在

    res$routes$overview_polyline$points
    # [1] "slviBqmm}MSLiA{B^wAj@sB}Ac@...
    

    这是一条编码折线。

    要从中获取纬度/经度,请使用函数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
    

    当然你可以随心所欲地绘制

    library(leaflet)
    
    leaflet() %>%
      addTiles() %>%
      addPolylines(data = df_polyline, lat = ~lat, lng = ~lon)
    


    编辑 2017-07-21

    googleway 2.0 开始,您可以在 Google 地图中绘制折线,可以像以前一样使用解码后的坐标,也可以直接使用折线

    google_map(key = key) %>%
        add_polylines(data = data.frame(polyline = res$routes$overview_polyline$points), 
                                    polyline = "polyline")
    

    【讨论】:

    • 问题标签用于捕捉问题的内容;请停止将与您的图书馆相关的标签添加到不询问您的图书馆的问题。 googleway 只属于专门针对该库的问题。
    • @josliber - 我解释了这篇元帖子 - tagging a question based on its answers 表示这样做是可以接受的。
    • 请参阅该帖子中的 meta.stackoverflow.com/questions/252079/… ——请仅在问题中询问时标记库/工具。
    • @josliber - 如果你把它写成一个答案,那么声明你是一个包的作者也是政策吗?如果是这样,我应该标记违反上述政策的答案吗?
    • 没有人应该回答包裹推荐问题 - 他们应该被关闭,因为他们正在询问包裹推荐。除此之外,一般规则是:如果有人询问包 X,那么包 X 的维护者可以在不透露隶属关系的情况下回答(他们不是在推广)。如果有人问了一个没有提到包 X 的问题,而包 X 的维护者建议包 X 来解决它,他们需要披露从属关系,并确保他们在回答中没有这样做。
    【解决方案2】:

    这基本上归结为创建一个route_df,然后将结果绘制为geom_path。例如,对于单个路由,您可以执行以下操作:

    library(ggmap)
    
    route_df <- route(from = "Hyderabad, Telangana 500085, India",
                      to = "Kukatpally, Hyderabad, Telangana 500072, India",
                      structure = "route")
    
    my_map <- get_map("Hyderabad, Telangana 500085, India", zoom = 13)
    
    ggmap(my_map) +
      geom_path(aes(x = lon, y = lat), color = "red", size = 1.5,
                data = route_df, lineend = "round")
    

    因此,您可能可以通过生成每条 from-to 路线并将所有结果 rbind-ing 到一个大的 route_df 并绘制最终结果来解决此问题。如果您尝试并显示(使用代码)您卡在哪里,其他人会更容易帮助您。您可能想要编辑您的原始问题,或者在展示您尝试过的内容后提交一个新问题。

    This SO postthis answer 应该会有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多