2016-10-29 88 views
0

我想創建一個腳本,人們可以輸入緯度和經度來顯示距離地點表最近的地方。使用「like」或「=」將顯示精確匹配的結果。但是,如果沒有完全匹配,我希望能夠顯示最近的地方。是否有任何方法可以在MySQL中計算距離? 請幫助。謝謝使用經度和緯度在mysql中計算距離

回答

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以1米的精度返回米數的距離。

否則,如果你喜歡快,但不準確的計算(僅適用於小的距離有效),您的使用勾股定理: Calculating distance (pythagoras) and running count in sql query

還有很多其他的公式來計算距離,你需要根據情況選擇一個精度和性能,你需要:

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

MySQL Great Circle Distance (Haversine formula)

+0

有一個錯誤:'這個函數[查詢2 ERROR]在其聲明中沒有DETERMINISTIC,NO SQL或READS SQL DATA,並且啓用二進制日誌記錄(您可能*想要使用安全性較低的log_bin_trust_function_creators變量)。我在之前的服務器中使用了完全相同的功能,沒有問題。現在,當我想遷移到Amazon RDS時,出現此錯誤 – HendraWD

+0

看起來亞馬遜RDS對此很嚴格。我可以通過創建新的參數組並將log_bin_trust_function_creators設置爲1來解決它。然後,我將MySQL RDS實例設置爲使用新創建的參數組,重新啓動它,然後重新運行此查詢。-_- – HendraWD

相關問題