【问题标题】:Calculating the distance between ZIP Codes in R with the mapdist function使用 mapdist 函数计算 R 中邮政编码之间的距离
【发布时间】:2018-08-19 13:04:33
【问题描述】:

我正在尝试使用 R 中依赖于 Google Geocoding API 的 mapdist 函数。有许多 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 对,该功能可以完美运行。谢谢。

【问题讨论】:

  • 所以...这可能是googleapis 的问题?它提供的 URL 找不到 '19111'。

标签: r mapping


【解决方案1】:

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

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"

【讨论】:

  • 这似乎解决了问题。我在美国境内的另外 50 个邮政编码上进行了尝试,但没有 NA。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
  • 2017-10-25
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多