經過很多研究並沒有達成解決方案之後,我不得不問。 如何計算Mysql中兩個長/緯度點之間的距離? (有效的方式?)計算兩個長/經緯度之間的距離
我還發現this的解決方案,但我不知道這@lat @long是,我沒有使用點,因爲彈簧數據的JPA犯規允許幾何類型(除了使用MysqlSpatial方言)
在Java中我正在計算是這樣的:
private double getDistanceFromLatLonInKm(double lat1,double lon1,double lat2,double lon2) {
int R = 6371; //Earth Radius
double dLat = degreeToRadiant(lat2-lat1);
double dLon = degreeToRadiant(lon2-lon1);
double a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(degreeToRadiant(lat1)) * Math.cos(degreeToRadiant(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return R * c; //Distance in km
}
而我的表/實體包含了經度緯度一列,另一個。
另一個問題,有沒有可能解決這個問題,如前所述與春季數據jpa?
我試着將Java代碼翻譯成Mysql,但距離很遠。
SELECT name, (6371 * (
2 * ATAN2(
SQRT(
SIN(RADIANS((48.3069400-latitude))/2) * SIN(RADIANS((48.3069400-latitude)/2)) +
COS(RADIANS(latitude)) * COS(RADIANS(48.3069400)) * SIN(RADIANS((48.3069400-longitude)/2)) *
SIN(RADIANS((48.3069400-longitude)/2))
),SQRT(
1-(SIN(RADIANS((48.3069400-latitude))/2) * SIN(RADIANS((48.3069400-latitude)/2)) +
COS(RADIANS(latitude)) * COS(RADIANS(48.3069400)) * SIN(RADIANS((48.3069400-longitude)/2)) *
SIN(RADIANS((48.3069400-longitude)/2)))
)
)
)) as distance FROM event
可能出現[找到兩個緯度/長點之間距離的最快方法](https://stackoverflow.com/questions/1006654/fastest-way-to-find-distance-between-two-lat-long-點) –
我已經鏈接到第三句話...... –
@ MarkusG.what是你的MySQL版本嗎? – olegsv