代码之家  ›  专栏  ›  技术社区  ›  John U

使用mapdist函数计算R中邮政编码之间的距离

  •  2
  • John U  · 技术社区  · 7 年前

    我正在尝试使用R中的mapdist函数,它依赖于Google地理编码API。有许多对ZIP,mapdist函数会为其生成NA值。例如:

    mapdist('19111', '19187')
    by using this function you are agreeing to the terms at :
    http://code.google.com/apis/maps/documentation/distancematrix/
    
    Information from URL : http://maps.googleapis.com/maps/api/distancematrix/json?origins=19111&destinations=19187&mode=driving&sensor=false
       from    to km miles minutes hours
    1 19111 19187 NA    NA      NA    NA
    

    但当我进入谷歌地图网站时,它很容易找到正确的答案,大约13英里。有人知道为什么会这样吗?对于许多其他拉链对,该功能可以完美工作。谢谢

    1 回复  |  直到 7 年前
        1
  •  1
  •   SymbolixAU Adam Erickson    7 年前

    可能是邮政编码本身不明确。如果您在搜索字符串中包含“USA”,它会起作用

    library(ggmap)
    
    mapdist(from = c("19111, USA"), to = c("19187, USA"))
    
    
    #         from         to     m    km    miles seconds  minutes     hours
    # 1 19111, USA 19187, USA 21420 21.42 13.31039    1976 32.93333 0.5488889
    

    library(googleway)
    
    set_key("your_api_key")
    
    google_distance(origins = c("19111, USA"), 
                    destinations = c("19187, USA"))
    
    # $destination_addresses
    # [1] "Philadelphia, PA 19187, USA"
    # 
    # $origin_addresses
    # [1] "Philadelphia, PA 19111, USA"
    # 
    # $rows
    # elements
    # 1 21.4 km, 21420, 33 mins, 1976, 35 mins, 2101, OK
    # 
    # $status
    # [1] "OK"