drop function getDistance;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` FUNCTION `getDistance`(
lon1 float(10,7)
,lat1 float(10,7)
,lon2 float(10,7)
,lat2 float(10,7)
) RETURNS double
begin
declare d double;
declare radius int;
set radius = 6378140; #假设地球为正球形,直径为6378140米
set d = (2*ATAN2(SQRT(SIN((lat1-lat2)*PI()/180/2)
*SIN((lat1-lat2)*PI()/180/2)+
COS(lat2*PI()/180)*COS(lat1*PI()/180)
*SIN((lon1-lon2)*PI()/180/2)
*SIN((lon1-lon2)*PI()/180/2)),
SQRT(1-SIN((lat1-lat2)*PI()/180/2)
*SIN((lat1-lat2)*PI()/180/2)
+COS(lat2*PI()/180)*COS(lat1*PI()/180)
*SIN((lon1-lon2)*PI()/180/2)
*SIN((lon1-lon2)*PI()/180/2))))*radius;
return d;
end
$$
DELIMITER ;
select getDistance(116.3899,39.91578,116.3904,39.91576); #调用函数
Mysql计算两GPS坐标的距离SQL语句:
#lat为纬度, lng为经度, 一定不要弄错
declare @lon1 float;
declare @lat1 float;
declare @lon2 float;
declare @lat2 float;
set @lon1=116.3899;
set @lat1=39.91578;
set @lon2=116.3904;
set @lat2=39.91576;
select (2*ATAN2(SQRT(SIN((@lat1-@lat2)*PI()/180/2)
*SIN((@lat1-@lat2)*PI()/180/2)+
COS(@lat2*PI()/180)*COS(@lat1*PI()/180)
*SIN((@lon1-@lon2)*PI()/180/2)
*SIN((@lon1-@lon2)*PI()/180/2)),
SQRT(1-SIN((@lat1-@lat2)*PI()/180/2)
*SIN((@lat1-@lat2)*PI()/180/2)
+COS(@lat2*PI()/180)*COS(@lat1*PI()/180)
*SIN((@lon1-@lon2)*PI()/180/2)
*SIN((@lon1-@lon2)*PI()/180/2))))*6378140;
关键字词:
相关文章
- mysql slave不能同步Last_SQL_Error Error Duplicate entry
- MySQL 提示Incorrect key file for table 'xx'; try to 出错无法打开
- MySQL Got error 28 from storage engine 解决办法
- 高可用的Mysql双机热备(Mysql_HA)
- mysql 对数据表的结构和数据进行复制
- mysql数据库表的错误 got error 28 from storage engine
- MySQL存储过程中使用动态行转列
- 查看运行MySQL中的语句(查询正在执行的sql)
- mysql安装图解 mysql图文安装教程(详细说明)
- Can't connect to MySQL server on localhost (10061)解决方法