Skip to content

Instantly share code, notes, and snippets.

@mirinzhang
Created December 11, 2020 11:23
Show Gist options
  • Select an option

  • Save mirinzhang/82e638afd195687e7db2595fed0e8cc4 to your computer and use it in GitHub Desktop.

Select an option

Save mirinzhang/82e638afd195687e7db2595fed0e8cc4 to your computer and use it in GitHub Desktop.
根据经纬度计算距离
function rad(d: number): number {
return (d * Math.PI) / 180;
}
function getDistanceByLatLng(lat1, lng1, lat2, lng2): number {
const EARTH_RADIUS = 6378137;
const r1 = rad(lat1);
const r2 = rad(lat2);
const a = r1 - r2;
const b = rad(lng1) - rad(lng2);
return EARTH_RADIUS * 2 * Math.asin(
Math.sqrt(
Math.pow(Math.sin(a / 2), 2) + Math.cos(r1) * Math.cos(r2) * Math.pow(Math.sin(b / 2), 2),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment