我想創建一個mysql函數來計算兩個地理代碼之間的半徑距離。
CREATE FUNCTION haversineDistance
(
lat1 VARCHAR(256),
long1 VARCHAR(256),
lat2 VARCHAR(256),
long2 VARCHAR(256)
)
RETURNS FLOAT DETERMINISTIC
BEGIN
DECLARE radLat1 FLOAT;
DECLARE radLong1 FLOAT;
DECLARE radLat2 FLOAT;
DECLARE radLong2 FLOAT;
DECLARE dLat FLOAT;
DECLARE dLong FLOAT;
DECLARE cordLength FLOAT;
DECLARE centralAngle FLOAT;
DECLARE distance FLOAT;
SET radLat1 = CAST(lat1 AS FLOAT) * PI()/180;
SET radLong1 = CAST(long1 AS FLOAT) * PI()/180;
SET radLat2 = CAST(lat2 AS FLOAT) * PI()/180;
SET radLong2 = CAST(long2 AS FLOAT) * PI()/180;
SET dLat = radLat2 - radLat1;
SET dLong = radLong2 - radLong2;
SET cordLength = POW(SIN(dLat/2),2)+COS(lat1)*COS(lat2)*POW(SIN(dLon/2),2);
SET centralAngle = 2 * ATAN2(SQRT(cordLength), SQRT(1-cordLength));
SET distance = 6367 * centralAngle;
RETURN distance;
END
但是我得到了這樣的錯誤;
1064 - 您的SQL語法錯誤;檢查手冊中 對應於你的MySQL服務器版本正確的語法使用 「」附近4行
任何人有一個想法?
預先感謝您。
如果我們知道第4行是什麼,它將幫助我們幫助您。 –