通过经纬度测量距离:
先要执行
SET GLOBAL log_bin_trust_function_creators = 1;
再执行以下代码:
DELIMITER $$
CREATE FUNCTION `dnDistance`(latitude1 DOUBLE, longitude1 DOUBLE, latitude2 DOUBLE, longitude2 DOUBLE) RETURNS double
BEGIN
DECLARE rad DOUBLE;
DECLARE radius DOUBLE DEFAULT 6378137;
DECLARE distance DOUBLE;
SET rad = PI() / 180.0;
SET latitude1 = latitude1 * rad;
SET longitude1 = longitude1 * rad;
SET latitude2 = latitude2 * rad;
SET longitude2 = longitude2 * rad;
SET distance = round(acos(cos(latitude1)*cos(latitude2)*cos(longitude1-longitude2)+sin(latitude1)*sin(latitude2))*radius*pow(10,3)) / pow(10,3);
RETURN distance;
END$$
DELIMITER ;
测试用的代码: 39.960004当前用户的经度,39.960004当前用户的维度, m.longitude商户的经度,m.latitude商户的维度
SELECT
(
dnDistance (
39.960004,
39.960004,
m.longitude,
m.latitude
) * 1
) AS distance
FROM
alq_hunter AS m
WHERE
id = 666