虽然这个问题已经有一段时间了,但我认为我可以为未来的人们提供帮助。
这个命令是我从Github上的一个脚本中学到的 - 最初由Peter Schmiedeskamp提交 - 这让我意识到R能够从Google Maps API中抓取驱车时间。我在我的博客示例中使用它来计算各种房屋销售到市中心位置的行车时间。
location
是包含每个观测值的长/宽坐标的列,格式如下:(-76.218922,36.841287)
。locMall
是我的数据集中带有每行商场的长/宽坐标的列。仅为澄清:该列中的每个单元格具有完全相同的值,而location
的每个单元格都不同。还有一些有用的信息:模式可以是driving
,walking
或bicycling
。
library(ggmap)
library(plyr)
google_results <- rbind.fill(apply(subset(sample, select=c("location", "locMall")), 1, function(x) mapdist(x[1], x[2], mode="driving")))
现在让我们来看一下结果:
head(google_results,4)
from to m km miles sec. minutes
1 (-76.219024, 36.901373) (-76.288018, 36.848950) 10954 10.954 6.806816 986 16.433333
2 (-76.243859, 36.868871) (-76.288018, 36.848950) 7279 7.279 4.523171 662 11.033333
3 (-76.296122, 36.859805) (-76.288018, 36.848950) 2101 2.101 1.305561 301 5.016667
4 (-76.264474, 36.938692) (-76.288018, 36.848950) 12844 12.844 7.981262 934 15.566667
hours
1 0.27388889
2 0.18388889
3 0.08361111
4 0.25944444
*编辑:更新。现在代码需要经度和纬度。
http://gis.stackexchange.com/
。 - user3710546