I am using google map map API
isLocationOnEdge
var isLocationNear = google.maps.geometry.poly.isLocationOnEdge(latlng, new google.maps.Polyline({
path: google.maps.geometry.encoding.decodePath(result.routes[0].overview_polyline)
}), .00001);
I dnt understand how tolerance is related to km
isLocationOnEdge(point:LatLng, poly:Polygon|Polyline, tolerance?:number)
Let sat i want to detect if a user is within 100m for any polyline drawn on map. How to fix this.
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.002, so if you use number 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;
};
The package for isLocationOnEdge which I found is as given below -
com.google.maps.android.PolyUtil.isLocationOnEdge()
It worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With