使用mysql中的纬度和经度计算距离

时间:2016-10-29 19:14:19

标签: mysql

我正在尝试创建一个脚本,人们可以在其中输入纬度和经度来显示距离place表最近的位置。使用“like”或“=”将显示获取完全匹配的结果。但是如果没有完全匹配,我希望能够显示最近的地方。在MySQL中有没有办法计算距离? 请帮忙。感谢

1 个答案:

答案 0 :(得分:1)

您可以使用The Vincenty Formula https://en.wikipedia.org/wiki/Vincenty%27s_formulae

在MySql中,您可以使用以下代码创建一个计算距离的函数:

DELIMITER $$
DROP FUNCTION IF EXISTS vincenty_distance$$
CREATE FUNCTION vincenty_distance(lat1 DOUBLE, lon1 DOUBLE, lat2 DOUBLE, lon2 DOUBLE) RETURNS DOUBLE
BEGIN
    DECLARE TO_RAD DOUBLE;
    DECLARE a INT;
    DECLARE b DOUBLE;
    DECLARE f DOUBLE;
    DECLARE L DOUBLE;
    DECLARE U1 DOUBLE;
    DECLARE U2 DOUBLE;
    DECLARE sinU1 DOUBLE;
    DECLARE cosU1 DOUBLE;
    DECLARE sinU2 DOUBLE;
    DECLARE cosU2 DOUBLE;
    DECLARE lambda DOUBLE;
    DECLARE lambdaP DOUBLE;
    DECLARE iterLimit INT;
    DECLARE sinLambda DOUBLE;
    DECLARE cosLambda DOUBLE;
    DECLARE sinSigma DOUBLE;
    DECLARE cosSigma DOUBLE;
    DECLARE sigma DOUBLE;
    DECLARE sinAlpha DOUBLE;
    DECLARE cosSqAlpha DOUBLE;
    DECLARE cos2SigmaM DOUBLE;
    DECLARE C DOUBLE;
    DECLARE D DOUBLE;
    DECLARE E DOUBLE;
    DECLARE uSq DOUBLE;
    DECLARE deltaSigma DOUBLE;
    DECLARE s DOUBLE;
    SET TO_RAD = pi() / 180; /*converts degree to raians*/
    SET a = 6378137;
    SET b = 6356752.3142;
    SET f = 1 / 298.257223563; /* WGS-84 ellipsoid params*/
    SET L = (lon2-lon1) * TO_RAD;
    SET U1 = atan((1 - f) * tan(lat1 * TO_RAD));
    SET U2 = atan((1 - f) * tan(lat2 * TO_RAD));
    SET sinU1 = sin(U1);
    SET cosU1 = cos(U1);
    SET sinU2 = sin(U2);
    SET cosU2 = cos(U2);
    SET lambda = L;
    SET iterLimit = 100;
    REPEAT
        SET sinLambda = sin(lambda);
        SET cosLambda = cos(lambda);
        SET sinSigma = sqrt((cosU2 * sinLambda) * (cosU2 * sinLambda) + (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda) * (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda));

        IF 0 = sinSigma THEN
            RETURN 0; /* co-incident points*/
        END IF;
        SET cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosLambda;
        SET sigma = atan2(sinSigma, cosSigma);
        SET sinAlpha = cosU1 * cosU2 * sinLambda / sinSigma;
        SET cosSqAlpha = 1 - sinAlpha * sinAlpha;
        IF (cosSqAlpha = 0) THEN
           SET cos2SigmaM = 0; /* equatorial line: cosSqAlpha = 0 (§6)*/
        ELSE
            SET cos2SigmaM = cosSigma - 2 * sinU1 * sinU2 / cosSqAlpha;
            SET C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
        END IF;
        SET lambdaP = lambda;
        SET lambda = L + (1 - C) * f * sinAlpha * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
        SET iterLimit = iterLimit - 1;
    UNTIL (abs(lambda - lambdaP) <= 0.0000000001 && iterLimit = 0) END REPEAT;
    SET uSq = cosSqAlpha * (a * a - b * b) / (b * b);
    SET D = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
    SET E = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
    SET deltaSigma = E * sinSigma * (cos2SigmaM + E / 4 * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - E / 6 * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
    SET s = b * D * (sigma - deltaSigma);

    RETURN round(s, 3); /* round to 1mm precision*/
END$$
DELIMITER ;

要使用,您只需要使用纬度和经度参数调用函数vincenty_distance,例如:

SELECT vincenty_distance(47.6593,10.97647,46.2512010,10.069972);

Vincenty公式是计算距离的最精确方法之一,因为它假定地球的形状为扁球体。

Alghorithm以米为单位返回距离,精度为1mm。

否则,如果您更喜欢更快但不准确的计算(仅在小距离上有效),您可以使用毕达哥拉斯定理: Calculating distance (pythagoras) and running count in sql query

还有很多其他公式来计算距离,您需要根据所需的准确度和性能选择一个:

https://en.wikipedia.org/wiki/Geographical_distance

MySQL Great Circle Distance (Haversine formula)