isLocationOnEdge tolerance calculation in terms of km

From one of the comments in this post:

tolerance, it is based on the decimal place accuracy desired in terms of latitude and longitude.

Example if say (33.00276, -96.6824) is on the polyline, if the tolerance is 0.00001 then if you change the point to (33.00278, -96.6824) then the point will ont be on the polyline.

So, you can probably use 0.001 as the tolerance value, if you want to find detect a location within about 100m for polyline.

For example, if your location is (1.001, 12), one of the points in polyline is(1, 12), the distance between your location and the polyline will be about 111.319 meters. The tolerance between (1.001, 12) and (1, 12) is 0.001, so the isLocationOnEdge() will return true.

If your location is (1.002, 12), distance to (1, 12), will be about 222.638 meters. The tolerance between them is 0.02, so if you use 0.001 as the tolerance value for isLocaitonOnEdge(), it will return false.

You can see the sample code from this JSFiddle: https://jsfiddle.net/j7cco3b0/1/


You can also create a custom function to validate in meters for a better precision.

var isLocationOnEdge=function(location,polyline,toleranceInMeters) {
    for(var leg of polyline.getPath().b) {
        if(google.maps.geometry.spherical.computeDistanceBetween(location,leg) <= toleranceInMeters){
            return true;
        }
    } 
    return false;
};