使用mapdist函数计算R中邮政编码之间的距离 [英] Calculating the distance between ZIP Codes in R with the mapdist function
问题描述
我正在尝试在R中使用mapdist函数,该函数依赖于Google Geocoding API.有许多对ZIP,其mapdist函数可得出NA值.例如:
I am trying to use the mapdist function in R which relies on the Google Geocoding API. There are many pairs of ZIPs for which the mapdist function results in NA values. For instance:
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
但是,当我进入Google Maps网站时,它很容易找到正确的答案,大约13英里.有人知道为什么会这样吗?对于许多其他的ZIP对,此功能可以完美运行.谢谢.
But when I go into the Google Maps website it easily finds the correct answer which is about 13 miles. Does anyone know why this is happening? For many other pairs of ZIPs the function works perfectly. Thanks.
推荐答案
邮编本身可能含糊不清.如果您在搜索字符串中包含美国",则可以使用
It could be that the zip code on its own is ambiguous. If you include 'USA' in the search string it works
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"
这篇关于使用mapdist函数计算R中邮政编码之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!