Javascript 使用gpx文件计算与谷歌地图的距离
我想计算一组地理坐标的距离。我已经把它转换成一个GPX文件,我可以在这里使用地图来计算距离Javascript 使用gpx文件计算与谷歌地图的距离,javascript,google-maps,distance,Javascript,Google Maps,Distance,我想计算一组地理坐标的距离。我已经把它转换成一个GPX文件,我可以在这里使用地图来计算距离 现在,我想根据我的客户要求在谷歌地图中使用它。谷歌地图中有接受GPX文件和返回距离的选项吗?我看到了distancematrix选项,相信这是不同的格式。这里有一些部分。计算基于 请注意,如果只需要计算距离,则甚至不需要GoogleMapsAPI。计算仅基于坐标 解析GPX文件 GPX基本上是一种xml和html。因此,在使用window.DOMParser.parseFromStringstr解析文件内
现在,我想根据我的客户要求在谷歌地图中使用它。谷歌地图中有接受GPX文件和返回距离的选项吗?我看到了distancematrix选项,相信这是不同的格式。这里有一些部分。计算基于 请注意,如果只需要计算距离,则甚至不需要GoogleMapsAPI。计算仅基于坐标 解析GPX文件 GPX基本上是一种xml和html。因此,在使用window.DOMParser.parseFromStringstr解析文件内容之后,text/xml;您可以使用DOM API(如querySelector、querySelectorAll等)检索所有trkpt元素并提取它们的lat和lon值 const coords=Array.fromxml.querySelectorAlltrkpt.map 元素=> 新google.maps.LatLng numberRelation.getAttributeLata, numberrelation.getAttributelon ; 我使用了google.maps.LatLng,但如果不需要它与地图交互,可以将其存储在普通对象中 计算距离 迭代坐标数组并从一个点测量到另一个点
function haversine_distance(coord1, coord2) {
const R = 3958.8; // Radius of the Earth in miles
const rlat1 = coord1.lat() * (Math.PI/180); // Convert degrees to radians
const rlat2 = coord2.lat() * (Math.PI/180); // Convert degrees to radians
const difflat = rlat2-rlat1; // Radian difference (latitudes)
const difflon = (coord2.lng()-coord1.lng()) * (Math.PI/180); // Radian difference (longitudes)
const d = 2 * R * Math.asin(Math.sqrt(Math.sin(difflat/2)*Math.sin(difflat/2)+Math.cos(rlat1)*Math.cos(rlat2)*Math.sin(difflon/2)*Math.sin(difflon/2)));
return d;
}
然后可以使用函数来构建距离数组
常数距离=coords.reduceold,ne,索引,原始=>{
如果索引>0{
old.pushhaversine_distancene,原始[索引-1];
}
返老还童;
}, [];
你怎么认为?