- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有数据框中两个点的纬度和经度。我在 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 中 origins
和 destinations
周围的单引号。当您使用代码将它们连接在一起时
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/
这个问题在这里已经有了答案: Measuring the distance between two coordinates in PHP (13 个答案) 关闭 9 年前。 我在数据库中存储了 fi
我想在 Google map 标记完成拖动时检索正确的 lat/lng。 但是对于同一个精确点,它给出了两个不同的(非常近似)坐标。 同一掉落的示例结果: evt.latLng:39.82213542
我有一个 mysql 插入语句,当系统无法获取用户的纬度/经度时,该语句会中断。sql 是这样的: INSERT INTO users(user_email, first_name , last_na
我有一个页面,用户可以在“邮政编码”字段中输入他们的地址。我还有一个“检测位置”按钮,可以对用户地址进行地理定位。当他们单击此按钮时,他们的城市名称和邮政编码将回显到“邮政编码”字段中,并且纬度和经度
我有两个表:一个 map 表,其中包含大约 3,000 个纬度/经度对,代表现实世界的“标记”位置;还有一个建筑物表,其中有数百个纬度/经度对,也代表真实世界的建筑物位置。我想做的是获取所有建筑物可见
假设我有一个 lat lng 坐标,我想把它放在一个 10 公里宽的正方形的中心,然后得到最小的 lat/lng 和最大的 lat/lng。 有没有一种简单的方法可以做到这一点已经存在? 最佳答案 如
在过去的几天里,我一直在努力寻找这个问题的答案。我有一个 GPS 数据路线表。和一张积分表。我需要的是一个 MySql 语句,对于给定的路线,将告诉我两个端点的 Point.Name。我可以写 4 个
我见过这个问题的许多变体,但我无法将他们的答案与我的具体需求联系起来。 我有几组 3 纬度/经度坐标对。任何集合中的坐标彼此之间都在几公里之内。 对于每个集合,我想将坐标转换为 x/y 值,以便我可以
我正在为 React 创建一个节点模块,只是为了尝试一下并了解更多信息。但我面临一个问题。我想让这个模块没有任何依赖项(React 除外),但我也想在我的模块中包含 I18n。我开始像这样的文件结构:
我使用mapbox/leaflet来显示人体图片而不是常规 map 。 我正在使用传单绘制,我需要能够创建一个圆并在保持其半径的同时移动它。然而,当我将其移向 map /屏幕底部时,大小呈指数级增加。
大家好,我目前正在 android 中使用 DH key 交换开发客户端服务器应用程序。我在客户端将字符串转换为 long 时遇到问题,奇怪的是该转换适用于前 2 个变量 pValue 和 gValu
我试图从数据库中的多点列获取值(纬度和经度)。我有针对点的有效解决方案,但找不到针对多点的解决方案。 我有一个点及其坐标的SQL查询,对于多点及其点我需要这样的东西: SELECT ASWKT(poi
我有一个保存地址的 mysql 记录数据库。我想迭代这些地址并获取每个地址的地理编码 lat 和 lng 并将它们放入数据库中。我正在使用 php。但是当我运行此代码时,我收到一条错误消息“FastC
我想要提取纬度。和来自字符串的经度值,如下所示: var data = "(-20.210696507479017, -70.14000177383423),(-20.21202551027535,
给定具有经纬度位置的地点数据库,例如 40.8120390、-73.4889650,我如何找到特定位置给定距离内的所有位置? 从数据库中选择所有位置,然后一个一个地遍历它们,获取与起始位置的距离,看它
我计划在我的一个应用程序中使用谷歌地图“containsLocation()”API。文档链接是 - https://goo.gl/4BFHCz 我正在使用相同的示例。这是我的代码。
我有 4 个变量: $southwest_lat、$southwest_lng、$northeast_lat、$northeast_lng 我还有一个数据库表,其中包含以下字段:名称、纬度和经度。 /
我正在尝试将标记数据放入表单上的隐藏字段中。我不确定为什么这不起作用,它一定是我的 js 语法中的某些内容: var initialLocation; var siberia = new google
我正在使用 Google map 函数 map.getCenter().lng(); 它返回 lng 值,但一旦我尝试对 lng 值执行任何操作,我就会收到错误,例如: var lang = sawd
假设我有几个带有 4 个坐标对(仅长/纬度)的边界框,每个坐标对代表一个方框的 4 个角。我如何检查其中两个框是否相交? 我知道我可以使用java.awt.Rectangle来检查两个矩形是否相交,但
我是一名优秀的程序员,十分优秀!