使用mapdist函数计算R中的ZIP代码之间的距离

时间:2018-03-11 11:31:27

标签: r mapping

我正在尝试使用R中的mapdist函数,该函数依赖于Google Geocoding 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英里的正确答案。有谁知道为什么会这样?对于许多其他ZIP对,该功能完美无缺。谢谢。

1 个答案:

答案 0 :(得分:1)

可能是邮政编码本身不明确。如果您包含' 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"