PaulLeCam / react-leaflet

React components for Leaflet maps
https://react-leaflet.js.org
Other
5.1k stars 884 forks source link

Get Distance Function? #1128

Open apexbb opened 6 months ago

apexbb commented 6 months ago

Do the package have the disatnce function between two co-ordinate ? like something below? //get the distance between two points let _length = map.distance(_firstLatLng, _secondLatLng);

raspacee commented 4 months ago

No the library does not provide any function to calculate distance between two coordinates. This library fails to provide any useful functionality or documentation. You need to be the developer of this library if you want to use this library. But you can use something like the haversine formula to get the distance between two coordinates. Below is a implementation of it that gives distance in kilometers

function haversine(lat1: number, lon1: number, lat2: number, lon2: number) {
  // distance between latitudes
  // and longitudes
  let dLat = ((lat2 - lat1) * Math.PI) / 180.0;
  let dLon = ((lon2 - lon1) * Math.PI) / 180.0;

  // convert to radiansa
  lat1 = (lat1 * Math.PI) / 180.0;
  lat2 = (lat2 * Math.PI) / 180.0;

  // apply formulae
  let a =
    Math.pow(Math.sin(dLat / 2), 2) +
    Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2);
  let rad = 6371;
  let c = 2 * Math.asin(Math.sqrt(a));
  return (rad * c).toFixed(1);
}
IanMayo commented 4 months ago

Ahem, I've successfully used this utility in my open-source projects for years, and I'm certainly not a repo developer.

A react-based wrapper for leafletjs shouldn't be expected to provide a capability such as that requested.

In the spirit of Open Source projects, I welcome your future documentation contribution. Thanks Bijay!