2

我需要验证坐标 LAT/LNG 点是否在其他两点之间(如道路的线段)。我按照这个主题没有成功。

function inRange(start, point, end) {
    start_x = start.lat();
    start_y = start.lng();
    end_x = end.lat();
    end_y = end.lng();
    point_x = point.lat();
    point_y = point.lng();

    var dx = end_x - start_x;
    var dy = end_y - start_y;
    var innerProduct = (point_x - start_x)*dx + (point_y - start_y)*dy;
    return 0 <= innerProduct && innerProduct <= dx*dx + dy*dy;
}


function checkRange(start, point, end){
    var x1 = start.lat();
    var y1 = start.lng();
    var x2 = end.lat();
    var y2 = end.lng();
    var x = point.lat();
    var y = point.lng();

    if (x1 == x2) {  // special case
        return y1 < y2 ? (y1 <= y && y <= y2) : (y2 <= y && y <= y1);
    }

    var m = (y2 - y1) / (x2 - x1);
    var r1 = x1 + m * y1;
    var r2 = x2 + m * y2;
    var r = x + m * y;
    return r1 < r2 ? (r1 <= r && r <= r2) : (r2 <= r && r <= r1);
}
  • 第一次测试

开始:(44.4963217, 11.327993300000003)

结束: (44.4973624, 11.32760170000006)

点 (44.4958434, 11.328122000000008)

InRange == false (OK) 所以 Point 成为新的起点

  • 第二次测试

开始:(44.4958434, 11.328122000000008)

结束: (44.4973624, 11.32760170000006)

点:(44.4966928,11.32781620000003)

InRange == false (ERROR) Point2 在开始/结束之间,但函数返回 false :(

4

1 回答 1

1

您的方法正在响应,因为它应该给出您传递的数据:

var start_lng = 11.328122000000008;

var end_lng = 11.32760170000006; // This longitude is less than the point longitude.

var point_lng = 11.32781620000003; // This longitude more than the end longitude.
于 2013-07-27T13:11:14.163 回答