Google地图确定沿线的距离 [英] Google Maps determine distance along Line

查看:80
本文介绍了Google地图确定沿线的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定Google地图中沿给定折线(从起点)的点的距离(假设用户单击折线,并且在事件中获得了点坐标).

I am trying to determine the distance of a point along a given Polyline (from the start point) in Google maps (given that the user clicks on the Polyline and I get the point coordinates in the event).

到目前为止,这是我唯一想到的:

So far, this is the only thing that comes to mind:

  • 遍历折线中的所有线段,直到找到一个这样的线段为止 d(line,point)〜= 0,跟踪到目前为止所覆盖的距离.
  • 在点所在的线段上进行插值以找到其距离 相对于段的开头.
  • Iterate over all segments in the Polyline until I find one such that d(line, point) ~= 0, keeping track of the distance covered so far.
  • Interpolate on the segment the point is on to find its distance relative to the start of the segment.

遗憾的是,对于某些应该直接执行的操作而言,这似乎相当复杂.

Sadly, this seems rather complicated for something that should be straightforward to do.

有没有更简单的方法?

P.S .:我正在使用API​​ v3

P.S.: I'm using API v3

推荐答案

因此,经过大量搜索,我决定实现上述算法.事实证明,这还没有我想像的那么糟糕.如果有人登陆此页面,则完整代码如下:

So, after much searching I decided to implement the algorithm as described above. Turned out it isn't as bad as I thought. Should anyone ever land on this page, the full code is below:

var DistanceFromStart = function (/*latlng*/ markerPosition) {

    var path = this.polyline.getPath();      
    var minValue = Infinity;
    var minIndex = 0;
    var x = markerPosition.lat();
    var y = markerPosition.lng();

    for (var i = 0; i < path.getLength() - 1; i++) {

        var x1 = path.getAt(i).lat();
        var y1 = path.getAt(i).lng();

        var x2 = path.getAt(i + 1).lat();
        var y2 = path.getAt(i + 1).lng();

        var dist = pDistance(x, y, x1, y1, x2, y2);

        if (dist < minValue) {
            minIndex = i;
            minValue = dist;
        }
    }      

    var gdist = google.maps.geometry.spherical.computeDistanceBetween;
    var dinit = gdist(markerPosition, path.getAt(minIndex));
    var dtotal = gdist(path.getAt(minIndex), path.getAt(minIndex + 1));

    var distanceFromStart = 0;

    for (var i = 0; i <= minIndex - 1; i++) {
        distanceFromStart += gdist(path.getAt(i), path.getAt(i + 1));
    }

    distanceFromStart += dtotal * dinit / dtotal;

    return distanceFromStart;
}

function pDistance(x, y, x1, y1, x2, y2) {

    var A = x - x1;
    var B = y - y1;
    var C = x2 - x1;
    var D = y2 - y1;

    var dot = A * C + B * D;
    var len_sq = C * C + D * D;
    var param = dot / len_sq;

    var xx, yy;

    if (param < 0 || (x1 == x2 && y1 == y2)) {
        xx = x1;
        yy = y1;
    }
    else if (param > 1) {
        xx = x2;
        yy = y2;
    }
    else {
        xx = x1 + param * C;
        yy = y1 + param * D;
    }

    var dx = x - xx;
    var dy = y - yy;
    return Math.sqrt(dx * dx + dy * dy);
}

如果您发现有任何改进之处,请告诉我.

If you see anything to improve, do let me know.

这篇关于Google地图确定沿线的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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