Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Distance Function? #1128

Open
apexbb opened this issue Feb 23, 2024 · 2 comments
Open

Get Distance Function? #1128

apexbb opened this issue Feb 23, 2024 · 2 comments

Comments

@apexbb
Copy link

apexbb commented Feb 23, 2024

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
Copy link

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
Copy link

IanMayo commented Apr 21, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants