欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql根据经纬度求两地距离

程序员文章站 2022-10-16 13:49:47
第一个函数是计算平面坐标系下,两点的距离,就是 如果用于计算地球两点的距离,带入的参数是角度(0-360度),则计算的单位也是相差的角度,用此角度计算距离不准。纬度距离约111km每度,经度距离在赤道平面上是111km每度,随纬度的升高逐渐降低为0。 第二个函数是计算球面距离的公式,传入的参数是经纬 ......
#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.

 

第一个函数是计算平面坐标系下,两点的距离,就是

  • mysql根据经纬度求两地距离

 如果用于计算地球两点的距离,带入的参数是角度(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.