Question

I am using Google Maps, and trying to find out if a point is between two other points. The way I've been doing it this far is making two vectors. One between the two "check" points, and one between one of the "check" points, and the "new" point. After the vectors have been calculated, i've crossed them, and gotten a cross value. Then i've done the same for all the "check" points i had, and if the new crossProduct was smaller than the old one, i've sat switched it out. This has been working soo far, but now i've ran into troubles that this can not solve. So I'm looking for another formular to find out if the point is between the other two points, with a margin of error.

Hope you can help me with this problem

Was it helpful?

Solution

Construct a polyline between the original two points. Then use the isLocationOnEdge function to determine if the 3rd point is within a certain number of degrees from your polyline.

e.g.

<!DOCTYPE html>
<html>
<head>
<title>Is a point within x degrees of a polyline</title>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { width:100%; height:100%; }
</style>
<!-- need to load the geometry library -->
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false"></script>

<script type="text/javascript">
    function initialize() {
        var latlng = new google.maps.LatLng(54.5003526, -3.0844116);

        var myOptions = {
            zoom: 10,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        var marker = new google.maps.Marker({
            position: latlng,
            map: map, 
            title: "foo"
        });

        var latLng1 = new google.maps.LatLng(54.60039, -3.13632);
        var latLng2 = new google.maps.LatLng(54.36897, -3.07561);

        var polyline = new google.maps.Polyline({
            path: [latLng1, latLng2],
            strokeColor: "#FF0000",
            strokeOpacity: 1.0,
            strokeWeight: 2,
            map: map
        });

        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline));
        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline, 1));
        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline, 0.1));
        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline, 0.01));
        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline, 0.001));
        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline, 0.0001));
        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline, 0.00001));
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>

</head>
<body>
    <div id="map_canvas"></div>
</body>
</html>

Doing this I can see my point is within 0.1 degree from my line, but it's not within 0.01 degree of it. Your tolerance in degrees will vary depending on situation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top