MySQL计算两个经纬度之间的距离函数
函数:
BEGIN
DECLARE dist int;
SET dist = round(6378.138*2*SIN(SQRT(POW(SIN((lat1*PI()/180-lat2*PI()/180)/2),2)+COS(lat1*PI()/180)*COS(lat2*PI()/180)* POW(SIN((lng1*PI()/180-lng2*PI()/180)/2),2)))*1000);
RETURN (dist);
END
mybatis xml中用法:FUN_JW_DIST为函数名,nowLng,nowLat为第一组经纬度,s.g_lng,s.g_lat为第二组经纬度
ifnull(ROUND(FUN_JW_DIST(#{nowLng},#{nowLat}, s.g_lng, s.g_lat)/1000,2),0) as distance
单位km