现在项目中使用了一个通过经纬度测量距离:
在Mysql中运行此代码,数据库中国会有一个函数名为dnDistance
MySQL数据库创建存储过程是遇到错误
you *might* want to use the less safe log_bin_trust_function_creators variable
先要执行
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 = 9