计算Google Maps API v3中折线(路线)和标记之间的距离 [英] Compute the distance between polyline (route) and marker in Google Maps API v3

查看:226
本文介绍了计算Google Maps API v3中折线(路线)和标记之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方便的方法计算Polyline(由Google Directions创建的路线)与不在该折线上的标记之间的直接距离(最短距离)?

我发现的唯一方法是手动循环 Polyline.getPath()顶点来计算最短距离,但它似乎有点苛刻:

  var path = routes [0] .overview_path; (var i = 0; i< data.points.length; i ++){
var latLngA = new LatLng(data.points [i] .lat,data.points [ I] .lng);
var shortest_distance = null; (var j = 0; j var distance = google.maps.geometry.spherical.computeDistanceBetween(latLngA,path [i]);

;

if(shortest_distance == null || distance< shortest_distance){
shortest_distance = distance;
}
}

console.log(data.points [i] .point_title,shortest_distance);
}

感谢您!

解决方案

据我所知,Google Maps API不能让您轻松完成此操作。不幸的是,你使用的算法不能给出准确的答案,因为它给出了从标记到路径上最近顶点的距离,而不是折线本身上的最近点,这通常不是其中的一个点。 / p>

如果您确实需要精确计算,我所知道的最佳选择是使用Javascript Topology Suite( JSTS )。 JSTS有很多地理公式来计算这种事情。这意味着将从方向API返回的折线转换为JSTS对象并调用正确的效用函数。不是微不足道的,但也不是太难。


Is there any convenient way to compute the direct (shortest) distance between Polyline (the route generated by Google Directions) and markers that are NOT situated on that polyline?

The only way I found out is to cycle through Polyline.getPath() vertices manually to calculate the shortest distance but it seems to be a bit harsh:

var path = routes[0].overview_path;

for (var i = 0; i < data.points.length; i++) {
    var latLngA = new LatLng(data.points[i].lat, data.points[i].lng);
    var shortest_distance = null;

    for (var j = 0; j < path.length; j++) {
        var distance = google.maps.geometry.spherical.computeDistanceBetween(latLngA, path[i]);

        if (shortest_distance == null || distance < shortest_distance) {
            shortest_distance = distance;
        }
    }

    console.log(data.points[i].point_title, shortest_distance);
}

Thanks in advance!

解决方案

As far as I know, the Google Maps API does not give you a way to do this easily. And unfortunately, the algorithm you use will not give an accurate answer, because it gives the distance from the marker to the closest vertex on the path, not the closest point on the polyline itself, which will usually not be one of the points.

If you really need an accurate calculation, the best option I know of is to use the Javascript Topology Suite (JSTS). JSTS has a ton of geographic formulas for calculating this sort of thing. That means converting the polyline returned from the directions API into a JSTS object and calling the right utility function. Not trivial, but not too difficult either.

这篇关于计算Google Maps API v3中折线(路线)和标记之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆