php数据库经纬度距离,PHP mysql 计算两个经纬度坐标距离

mysql版

SELECT 2 * 6378.137 * ASIN( SQRT( POW( SIN( PI( ) * ( 114.09576416015625 - 114.1798 ) /360 ) , 2 ) + COS( PI( ) * 114.09576416015625 /180 ) * COS( 114.1798 * PI( ) /180 ) * POW( SIN( PI( ) * ( 22.3932534047735 - 114.1798 ) /360 ) , 2 ) ) ) AS a

FROM dual

php版

function rad($d)

{

return $d * 3.1415926535898 / 180.0;

}

function GetDistance($lat1, $lng1, $lat2, $lng2)

{

$EARTH_RADIUS = 6378.137; //公里

$radLat1 = rad($lat1);

$radLat2 = rad($lat2);

$a = $radLat1 – $radLat2;

$b = rad($lng1) – rad($lng2);

$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 * 10000) / 10000;

return $

mysql版

CREATE DEFINER=`root`@`localhost` FUNCTION `distance`(lat1 float,lng1 float,lat2 float,lng2 float) RETURNS float

BEGIN

set @num=

2 * 6378.137* ASIN(SQRT(POW(SIN(PI() * (lat1 – lat2) / 360), 2) + COS(PI() * lat1 / 180)

* COS(lat2* PI() / 180) * POW(SIN(PI() * (lng1 – lng2) / 360), 2)));

RETURN @num;

END

google地图提供的方法(PHP版本)

class Distance{

const EARTH_RADIUS = 6378.137; // earth radius (const) kilometer

/**

*

*Example:

*$precision = 49;

*ini_set("precision", $precision);

*echo pi(); //will output 3.141592653589793115997963468544185161590576171875

*

*1 kilometer = 0.621371192 mile

*

*@param double d

*@return double data

*/

private function rad( $d )

{

return $d * M_PI / 180.0;

}

/**

*

*Example : get_distance(44.2112,-88.4175,34.5082,-82.6498)

*Get Distance between two point : longitude,latitude(lat1,lng1 => lat2,lng2 )

*

*@param double d

*@return double data

*

*/

public function get_distance( $lat1, $lng1, $lat2, $lng2, $base = 1000 )

{

$radLat1 = floatval( $this->rad($lat1) );

$radLat2 = floatval( $this->rad($lat2) );

$a = $radLat1 - $radLat2;

$b = $this->rad($lng1) - $this->rad($lng2);

$s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));

$s = $s * self::EARTH_RADIUS;

$s = round($s * 10000) / 10000;

return $s;

}

}

参看:

http://dev.mysql.com/doc/refman/5.1/zh/spatial-extensions-in-mysql.html

http://blog.csdn.net/aggrelxf/article/details/12054513

http://blog.sina.com.cn/s/blog_7bbfd5fd01017d1e.html

http://lesorb.iteye.com/blog/1945755

http://digdeeply.org/archives/06152067.html

你可能感兴趣的:(php数据库经纬度距离)