1. 程式人生 > >mysql根據經緯度求兩地距離

mysql根據經緯度求兩地距離

#1.兩點距離(1.4142135623730951)
select st_distance(point(0,0),point(1,1));
select st_distance(point (120.10591, 30.30163),point(120.13026,30.25961));
mysql 5.6 新增 #2.兩點球面距離(
157249.0357231545m) select st_distance_sphere(point(0,0),point(1,1)); select st_distance_sphere(point (120.10591, 30.30163),point(120.13026,30.25961));
This function was added in MySQL 5.7.6.

 

第一個函式是計算平面座標系下,兩點的距離,就是

  • formula

 如果用於計算地球兩點的距離,帶入的引數是角度(0-360度),則計算的單位也是相差的角度,用此角度計算距離不準。緯度距離約111km每度,經度距離在赤道平面上是111km每度,隨緯度的升高逐漸降低為0。

 

第二個函式是計算球面距離的公式,傳入的引數是經緯度(經度-180~180,緯度-90~90),返回的值以m為單位的距離。

ST_Distance_Sphere(g1g2 [, radius])

Returns the mimimum spherical distance between two points and/or multipoints on a sphere, in meters, or NULL

 if any geometry argument is NULL or empty.

Calculations use a spherical earth and a configurable radius. The optional radius argument should be given in meters. If omitted, the default radius is 6,370,986 meters. An ER_WRONG_ARGUMENTS error occurs if the radius

 argument is present but not positive.

The geometry arguments should consist of points that specify (longitude, latitude) coordinate values:

  • Longitude and latitude are the first and second coordinates of the point, respectively.

  • Both coordinates are in degrees.

  • Longitude values must be in the range (-180, 180]. Positive values are east of the prime meridian.

  • Latitude values must be in the range [-90, 90]. Positive values are north of the equator.

Supported argument combinations are (PointPoint), (PointMultiPoint), and (MultiPointPoint). An ER_GIS_UNSUPPORTED_ARGUMENT error occurs for other combinations.

If any geometry argument is not a syntactically well-formed geometry byte string, an ER_GIS_INVALID_DATA error occurs.

mysql> SET @pt1 = ST_GeomFromText('POINT(0 0)'); mysql> SET @pt2 = ST_GeomFromText('POINT(180 0)'); mysql> SELECT ST_Distance_Sphere(@pt1, @pt2); +--------------------------------+ | ST_Distance_Sphere(@pt1, @pt2) | +--------------------------------+ | 20015042.813723423 | +--------------------------------+

This function was added in MySQL 5.7.6.