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.

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

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

原文地址:https://www.cnblogs.com/hdwang/p/9994153.html

时间: 2024-10-09 05:00:45

mysql根据经纬度求两地距离的相关文章

根据两点的经纬度计算两地距离

学着做项目需要根据很据给出的两点经纬度信息计算两地的实际距离,查了查,有公式奉上: google地图提供的方法: 相关c#代码如下 private static double rad(double d) { return d * Math.PI / 180.0; } //根据两点的经纬度计算两地距离 public static double GetDistance(double lon1, double lat1, double lon2, double lat2) { double radLa

【微信开发】微信小程序通过经纬度计算两地距离php代码实现

需求: 要求做个根据用户当前位置获取周围商家地址,并且按照由近到远排序, 方法一: 代码层实现 封装方法: /** * @desc 根据两点间的经纬度计算距离 * @param float $lat 纬度值 * @param float $lng 经度值 * @param $status true KM,M显示;false 只返回M */ function system_getdistance($lat1, $lng1, $lat2, $lng2, $status = true, $single

php通过两个地点经纬度求距离

<?php /** *求两个已知经纬度之间的距离,单位为米 *@param lng1,lng2 经度 *@param lat1,lat2 纬度 *@return float 距离,单位米 *@author yalong sun<[email protected]> **/ function getdistance($lng1,$lat1,$lng2,$lat2){ //将角度转为狐度 $radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为弧度 $radLa

根据经纬度查询最近距离,mysql查询经纬度附近范围

public class Test{ private static List<LocalAddress> ilist = new ArrayList<LocalAddress>(); public static void main(String[] args) { Test test3 = new Test(); Double localDouble = 0.0; //定义一个二维数组存放经纬度 Double[][] doubles = { { 22.6036906766, 113

IOS 计算两个经纬度之间的距离

一 丶 -(double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(double) lng2{ CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1]; CLLocation *otherLocation = [[CLLocation alloc] initWithLatitude:lat2

根据一个经纬度求几公里范围内的数据

/** *求两个已知经纬度之间的距离,单位为米 *@param lng1,lng2 经度 *@param lat1,lat2 纬度 *@return float 距离,单位米 **/ function getDistance($lng1,$lat1,$lng2,$lat2){ //将角度转为狐度 $radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为弧度 $radLat2=deg2rad($lat2); $radLng1=deg2rad($lng1); $radL

【转】通过经纬度坐标计算距离的方法(经纬度距离计算)

最近在网上搜索“通过经纬度坐标计算距离的方法”,发现网上大部分都是如下的代码: #define PI 3.14159265 static double Rc = 6378137;  // 赤道半径 static double Rj = 6356725;  // 极半径 class JWD { public: double m_Longitude, m_Latitude; double m_RadLo, m_RadLa; double Ec; double Ed; public: JWD(doub

sql 计算两经纬度间的距离

DECLARE @g geography;SET @g = geography::STPointFromText('POINT(113.216273 23.236333)', 4326);SELECT @g.ToString();DECLARE @gg geography;SET @gg = geography::STPointFromText('POINT(115.567368 37.341209)', 4326);SELECT @gg.ToString(); SELECT @g.STDist

.NET资料之-根据两点经纬度计算直线距离

最近做东西碰到要根据两点经纬度计算之间的直线距离,就网上找了查了下资料.因为这类接触的比较少,就直接找现成的代码了,没怎么研究.代码如下,作为记录. private const double EARTH_RADIUS = 6378.137;//地球半径 private static double rad(double d) { return d * Math.PI / 180.0; } public static double GetDistance(double lat1, double ln