CREATE DEFINER=`qxdjadmin`@`%` FUNCTION `calc_distance`(`lons1` VARCHAR(10),`lats1` VARCHAR(10),`lons2` VARCHAR(10),`lats2` VARCHAR(10)) RETURNS double
BEGIN
#计算地图距离
#`lon1` ,`lat1`为列表坐标 lon2`,`lat2为查询坐标
DECLARE ew1 double;
DECLARE ew2 double;
DECLARE ns1 double;
DECLARE ns2 double;
DECLARE distance double;
DECLARE DEF_PI180 double;
DECLARE DEF_R double;
DECLARE lon1 double;
DECLARE lat1 double;
DECLARE lon2 double;
DECLARE lat2 double;
set DEF_PI180=0.01745329252;
set DEF_PI180=0.01745329252;
set DEF_R=6370693.5;
IF(lons1=NULL or lons1='') THEN SET lons1='0'; End if;
IF(lons2=NULL or lons2='') THEN SET lons2='0'; End if;
IF(lats1=NULL or lats1='') THEN SET lats1='0'; End if;
IF(lats2=NULL or lats2='') THEN SET lats2='0'; End if;
set lon1=cast(lons1 as decimal(10,6));
set lat1=cast(lats1 as decimal(10,6));
set lon2=cast(lons2 as decimal(10,6));
set lat2=cast(lats2 as decimal(10,6));
SET distance=0;
SET ew1 = lon1 * DEF_PI180;
SET ns1 = lat1 * DEF_PI180;
SET ew2 = lon2 * DEF_PI180;
SET ns2 = lat2 * DEF_PI180;
SET distance =SIN(ns1) * SIN(ns2) + COS(ns1) * COS(ns2) * COS(ew1 - ew2);
IF(distance>1.0) THEN SET distance=1.0; End if;
IF (distance<-1.0) THEN SET distance =-1.0; End if;
SET distance =round(ABS(DEF_R * ACOS(distance)+0.5),0);
RETURN distance;
END