'Leaflet: How to calculate distance from center point to the edges
I am using leaflet draw control get geometries of certain areas. These geometries are polygons, line-string, rectangles and circles and I am putting them in a database.I want to get the distance from center point to the edges of these geometries.
I am using https://github.com/mapbox/leaflet-omnivore to parse the geometry in this way:
var layer = omnivore.wkt.parse(geometry);
var center = layer.getBounds().getCenter();
I don't know how to get distance from center to edges. Is it possible?
Solution 1:[1]
For polygons, polylines and rectangles you can iterate through layer points (using layer.getLatLngs()) and calculate distance to vertices using L.LatLng.distanceTo() method or calculate distance to edges by using L.LineUtil.pointToSegmentDistance().
Circles are more tricky, because Leaflet draws circles in current map projection, not as circles on sphere/ellipsoid. So, geodesic distance from center to really drawn circle differs for different circle points. You can use L.Circle.getRadius() as approximation and distance to bound edges as minimum and maximum of the distance.
Solution 2:[2]
If you have two colliders and want to get minimum distance between them, you may want to use Physics.Distance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alexander Parshin |
| Solution 2 | FrameMuse |

