Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anything in Leaflet that is similar to isLocationOnEdge() from Google Maps?

Tags:

geo

leaflet

Google Maps has the function isLocationOnEdge(point, polyline, tolerance) that takes a tolerance value in degrees and uses it to determine whether a point falls near a polyline.

Is there anything similar in Leaflet(or some plug-in) that does the same thing?

like image 848
eych Avatar asked Oct 16 '25 15:10

eych


1 Answers

A handful library for such operation is Turf.

For your case, a simple approach would be to:

  1. Create a polygon out of your polyline using turf.buffer with appropriate "tolerance" (Turf takes a distance at Earth surface, or degrees).
  2. Check whether your point is within that polygon or not using turf.inside.

Unfortunately, turf.buffer is only an approximation, it does not takes geodesy into account… therefore for big tolerance you will have a deformed shape.

An exact method could be to:

  1. Use instead turf.pointOnLine to find the nearest point of the polyline.
  2. turf.distance to measure the distance between those 2 points, and compare with your tolerance (or even just Leaflet latLng.distanceTo, but you would have to convert GeoJSON points back to Leaflet LatLngs).
like image 95
ghybs Avatar answered Oct 19 '25 15:10

ghybs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!