/** * 距离计算 6378137地球半径 米 * @param unknown_type $lat1 * @param unknown_type $lng1 * @param unknown_type $lat2 * @param unknown_type $lng2 * @return number */ public static function getDistance($lat1, $lng1, $lat2, $lng2) { $radLat1 = self::rad($lat1); $radLat2 = self::rad($lat2); $a = $radLat1 - $radLat2; $b = self::rad($lng1) - self::rad($lng2); $s = 2 * Asin(Sqrt(Pow(Sin($a/2),2) + Cos($radLat1)*Cos($radLat2)*Pow(Sin($b/2),2))); $s = $s * 6378137; $s = Round($s * 1000) / 1000; return $s; }
public static function distanceSimplify($lat1, $lng1, $lat2, $lng2) { $dx = $lng1 - $lng2; // 经度差值 $dy = $lat1 - $lat2; // 纬度差值 $b = ($lat1 + $lat2) / 2.0; // 平均纬度 $Lx = deg2rad($dx) * 6378137 * cos(deg2rad($b)); // 东西距离 $Ly = 6378137 * deg2rad($dy); // 南北距离 return sqrt($Lx * $Lx + $Ly * $Ly); // 用平面的矩形对角距离公式计算总距离 }
distanceSimplifyMore这个未实现
public static double distanceSimplifyMore(double lat1, double lng1, double lat2, double lng2, double[] a) {
//1) 计算三个参数
double dx = lng1 - lng2; // 经度差值
double dy = lat1 - lat2; // 纬度差值
double b = (lat1 + lat2) / 2.0; // 平均纬度
//2) 计算东西方向距离和南北方向距离(单位:米),东西距离采用三阶多项式
double Lx = (a[3] * b*b*b + a[2]* b*b +a[1] * b + a[0] ) * toRadians(dx) * 6367000.0; // 东西距离
double Ly = 6367000.0 * toRadians(dy); // 南北距离
//3) 用平面的矩形对角距离公式计算总距离
return Math.sqrt(Lx * Lx + Ly * Ly);
}
经我测试,double[] a即使模拟测试,假设a[3]=1,a[2]=1,a[1]=1,a[0]=1去执行 100W次
$num = 1000000; G('begin'); for($i = 0; $i < $num; $i++){ $this->getDistance('38.016141','114.56136','36.137427','120.437623'); } G('end'); echo 'getDistance'.G('begin','end',6).'s'.PHP_EOL; G('begin'); for($i = 0; $i < $num; $i++){ $this->distanceSimplify('38.016141','114.56136','36.137427','120.437623'); } G('end'); echo 'distanceSimplify'.G('begin','end',6).'s'.PHP_EOL; G('begin'); for($i = 0; $i < $num; $i++){ $this->distanceSimplifyMore('38.016141','114.56136','36.137427','120.437623'); } G('end'); echo 'distanceSimplifyMore'.G('begin','end',6).'s'.PHP_EOL; die;
getDistance2.288000s
distanceSimplify1.308000s
distanceSimplifyMore1.187000s
distanceSimplify与getDistance有明显效果,而distanceSimplifyMore效果不明显