本文介绍了检查两点之间的坐标点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证坐标LAT / LNG点是否在其他两点之间(像道路一样的线段)。
我遵循没有成功。

 函数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;
返回0< = innerProduct&& innerProduct


函数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){//特殊情况
返回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;
返回r1< r2? (r1≤r&r≤r2):(r2≤r&r≤r1);
}




  • 首次测试


开始:(44.4963217,11.327993300000003)


$ b $结束:(44.4973624,11.32760170000006)



点(44.4958434,11.328122000000008)

InRange == false(OK)
所以Point成为新的起点Point 第二次测试


开始:(44.4958434, (44.4973624,11.32760170000006)



点:(44.4966928,11.32781620000003)


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

 >解决方案

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

var end_lng = 11.32760170000006; //此经度小于经度点

var point_lng = 11.32781620000003; // This long比末尾的经度还要多。


I need to verify if a coordinate LAT/LNG point is between other two points (a segment line like a road).I followed THIS topic with no success.

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);
}

  • First test

Start: (44.4963217, 11.327993300000003)

End: (44.4973624, 11.32760170000006)

Point (44.4958434, 11.328122000000008)

InRange == false (OK)So Point becomes new start Point

  • Second test

Start: (44.4958434, 11.328122000000008)

End: (44.4973624, 11.32760170000006)

Point: (44.4966928, 11.32781620000003)

InRange == false (ERROR)Point2 is between start/end but the function returns false :(

解决方案

Your method is responding as it should given the data you've passed it:

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.

这篇关于检查两点之间的坐标点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:43