2

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 :(

Community
  • 1
  • 1
Gorgo
  • 456
  • 1
  • 7
  • 19
  • I feel like you've over complicated this. Forgetting the 'special case' for the moment: what does `return (point.lat() < end.lat() && point.lat() > start.lat() && point.lng() < end.lng() && point.lng > start.lng());` return? – shennan Jul 27 '13 at 12:52
  • Your longitudinal point on the second test is not between the start and end longitudes. `(11.32781620000003 < 11.32760170000006 && 11.32781620000003 > 11.328122000000008)` returns false. – shennan Jul 27 '13 at 13:07
  • True, but if you check on GMaps you'll see that second test point is on the road between start and end points. – Gorgo Jul 27 '13 at 13:10
  • That's academic. The point is that your method is responding to the variables you've passed it as it should do. The problem, then, is with GMaps rendering or calculation of the position. – shennan Jul 27 '13 at 13:12

1 Answers1

1

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.
shennan
  • 10,798
  • 5
  • 44
  • 79
  • Agree on that. Any ideas on a working algorithm for this issue? – Gorgo Jul 27 '13 at 13:23
  • You'll have to go into a bit more detail about how you're getting the data and why you're expecting it to be different. Where are you plotting these coordinates? Is there a tool where you can see all three coordinates rendered on GMaps at any one time? – shennan Jul 27 '13 at 13:39
  • We're developing a CityNotifier with GoogleMaps. Users can notify events on map and I use traffic notifications to draw heatmpap overlays. [Here](http://ltw1306.web.cs.unibo.it)'s the project site. I use [THIS](http://itouchmap.com/latlong.html) site to check points from coordinates. Do I answer your question? :) – Gorgo Jul 27 '13 at 14:10
  • 1
    Yes that all makes sense. Unfortunately I'm about to leave for a wedding party so I won't be able to look for a while. – shennan Jul 27 '13 at 14:14