Created
July 24, 2017 09:15
-
-
Save changs1986/786b82a3757189e5fbabb83166b40c9a 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
| /** | |
| * 计算两个坐标之间的距离(米) | |
| * @param float $fP1Lat 起点(纬度) | |
| * @param float $fP1Lon 起点(经度) | |
| * @param float $fP2Lat 终点(纬度) | |
| * @param float $fP2Lon 终点(经度) | |
| * @return int | |
| */ | |
| function distanceBetween($fP1Lat, $fP1Lon, $fP2Lat, $fP2Lon){ | |
| $fEARTH_RADIUS = 6378137; | |
| //角度换算成弧度 | |
| $fRadLon1 = deg2rad($fP1Lon); | |
| $fRadLon2 = deg2rad($fP2Lon); | |
| $fRadLat1 = deg2rad($fP1Lat); | |
| $fRadLat2 = deg2rad($fP2Lat); | |
| //计算经纬度的差值 | |
| $fD1 = abs($fRadLat1 - $fRadLat2); | |
| $fD2 = abs($fRadLon1 - $fRadLon2); | |
| //距离计算 | |
| $fP = pow(sin($fD1/2), 2) + | |
| cos($fRadLat1) * cos($fRadLat2) * pow(sin($fD2/2), 2); | |
| return intval($fEARTH_RADIUS * 2 * asin(sqrt($fP)) + 0.5); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment