Last active
December 3, 2019 10:06
-
-
Save wujunze/0e67a769fca49cc435e79b5920102b09 to your computer and use it in GitHub Desktop.
php根据经纬度计算两点直接的距离
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
<?php | |
/** | |
* 计算两个坐标之间的距离(米) | |
* @param float $fP1Lat 起点(纬度) | |
* @param float $fP1Lon 起点(经度) | |
* @param float $fP2Lat 终点(纬度) | |
* @param float $fP2Lon 终点(经度) | |
* @return int | |
*/ | |
function distanceBetween($mylonlat, $findlonlat){ | |
$mylonlat = explode(',', $mylonlat); | |
$findlonlat = explode(',', $findlonlat); | |
list($lng1,$lat1) = $mylonlat; | |
list($lng2,$lat2) = $findlonlat; | |
$EARTH_RADIUS=6378.137; | |
$PI=3.1415926; | |
$radLat1 = $lat1 * $PI / 180.0; | |
$radLat2 = $lat2 * $PI / 180.0; | |
$a = $radLat1 - $radLat2; | |
$b = ($lng1 * $PI / 180.0) - ($lng2 * $PI / 180.0); | |
$s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2))); | |
$s = $s * $EARTH_RADIUS; | |
$s = round($s * 1000); | |
if ($len_type > 1) { | |
$s /= 1000; | |
} | |
$distance = round($s/1000,2); | |
return $distance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$len_type 哪来的