gpt4 book ai didi

xml - 从 Lat Lng 到 Loop 的行驶距离

转载 作者:数据小太阳 更新时间:2023-10-29 02:29:08 26 4
gpt4 key购买 nike

我有数据框中两个点的纬度和经度。我在 R 中使用下面的代码来获取行驶距离。

library(XML)
library(RCurl)
latlon2ft <- function(origin,destination){
xml.url <- paste0('http://maps.googleapis.com/maps/api/distancematrix/xml?origins=',origin,'&destinations=',destination,'&mode=driving&sensor=false')
xmlfile <- xmlParse(getURL(xml.url))
dist <- xmlValue(xmlChildren(xpathApply(xmlfile,"//distance")[[1]])$value)
distance <- as.numeric(sub(" km","",dist))
ft <- distance*3.28084 # FROM METER TO FEET
return(ft)
}

test$origin1 = paste0("'",test$store_lat,",",test$store_lng,"'")
test$destination1 = paste0("'",test$lat,",",test$lng,"'")

distance <- list()
for(i in 1:nrow(test)){
dat <- latlon2ft(test[i,'origin1'],test[i,'destination1'])
distance[[i]] <- dat
}

all_distance <- do.call("rbind", distance)

但我收到以下错误。

Error in xpathApply(xmlfile, "//distance")[[1]] : subscript out of bounds 
3 xmlChildren(xpathApply(xmlfile, "//distance")[[1]])
2 xmlValue(xmlChildren(xpathApply(xmlfile, "//distance")[[1]])$value)
1 latlon2ft(test[i, "origin1"], test[i, "destination1"])

这是我的数据样本:

store_lat | store_lng |     lat    |    lng 
19.21368 | 72.99034 | 19.1901094 | 72.9758546
19.10749 | 72.86444 | 19.1052534 | 72.8609213
19.01480 | 72.84545 | 18.9942502 | 72.8365256
19.01480 | 72.84545 | 19.1453449 | 72.8367015

我的代码哪里错了?据我所知,我无法在运行循环时将值正确传递到函数中。但我无法找到解决方法。在此先感谢您的帮助。

最佳答案

发生下标越界错误是因为您没有为 URL 使用正确的 API 格式。它从以下 Google 服务器生成 xmlfile 响应:

<?xml version="1.0" encoding="UTF-8"?>
<DistanceMatrixResponse>
<status>OK</status>
<origin_address/>
<destination_address/>
<row>
<element>
<status>NOT_FOUND</status>
</element>
</row>
</DistanceMatrixResponse>

其中没有有效的distance

错误是 URL 中 originsdestinations 周围的单引号。当您使用代码将它们连接在一起时

test$origin1 = paste0("'",test$store_lat,",",test$store_lng,"'")
test$destination1 = paste0("'",test$lat,",",test$lng,"'")

您在值周围添加了 ' 单引号,这是不正确的。如果你去掉单引号:

test$origin1 = paste0(test$store_lat,",",test$store_lng)
test$destination1 = paste0(test$lat,",",test$lng)

然后您的代码会生成一个正确的网址 http://maps.googleapis.com/maps/api/distancematrix/xml?origins=19.21368,72.99034&destinations=19.1901094,72.9758546&mode=driving&sensor=false" 没有单引号。Google 服务器返回的结果 XML 是:

<?xml version="1.0" encoding="UTF-8"?>
<DistanceMatrixResponse>
<status>OK</status>
<origin_address>External Bypass Rd, Laxmi Nagar, Balkum Pada, Majiwada, Thane, Maharashtra 400608, India</origin_address>
<destination_address>A-5, Chhatraprati Sambhaji Rd, Ghantali, Thane West, Thane, Maharashtra 400602, India</destination_address>
<row>
<element>
<status>OK</status>
<duration>
<value>981</value>
<text>16 mins</text>
</duration>
<distance>
<value>4908</value>
<text>4.9 km</text>
</distance>
</element>
</row>
</DistanceMatrixResponse>

现在有一个有效的距离值。

您可以找到有关 Google Maps Distance API here 的细节的更多详细信息

关于xml - 从 Lat Lng 到 Loop 的行驶距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36482506/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com