Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
php根据经纬度计算两点直接的距离
<?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;
}
@never615

This comment has been minimized.

Copy link

@never615 never615 commented Dec 3, 2019

$len_type 哪来的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment