Created
December 11, 2020 11:23
-
-
Save mirinzhang/82e638afd195687e7db2595fed0e8cc4 to your computer and use it in GitHub Desktop.
根据经纬度计算距离
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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