我需要验证坐标LAT / LNG点是否在其他两个点之间(像道路一样的线段)。 我跟着THIS topic没有成功。
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)
Point(44.4958434,11.328122000000008)
InRange == false(OK) 所以Point成为新的起点
开始:(44.4958434,11.328122000000008)
结束:(44.4973624,11.32760170000006)
点:(44.4966928,11.32781620000003)
InRange == false(错误) Point2在start / end之间,但函数返回false :(
答案 0 :(得分: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.