DELIMITER $$ CREATE FUNCTION `test`.`getDistance`(LatBegin FLOAT(10,4), LngBegin FLOAT(10,4), LatEnd FLOAT(10,4), LngEnd FLOAT(10,4))RETURNS FLOAT(10,4) BEGIN DECLARE Distance FLOAT(10,4) DEFAULT 0.0000; DECLARE EARTH_RADIUS FLOAT(10,4) DEFAULT 0.0000; DECLARE RadLatBegin FLOAT(10,4); DECLARE RadLatEnd FLOAT(10,4); DECLARE RadLatDiff FLOAT(10,4); DECLARE RadLngDiff FLOAT(10,4); SET EARTH_RADIUS = 6378.137; SET RadLatBegin = LatBegin *PI()/180.0; SET RadLatEnd = LatEnd *PI()/180.0; SET RadLatDiff = RadLatBegin - RadLatEnd; SET RadLngDiff = LngBegin *PI()/180.0 - LngEnd *PI()/180.0; SET Distance = 2 *ASIN(SQRT(POWER(SIN(RadLatDiff/2), 2)+COS(RadLatBegin)*COS(RadLatEnd)*POWER(SIN(RadLngDiff/2), 2))); SET Distance = Distance * EARTH_RADIUS; -- SET @Distance = Round(@Distance * 10000) / 10000 RETURN Distance; END$$ DELIMITER ;