Java计算两坐标点的距离:
/**
* 计算两经纬度点之间的距离(单位:米)
*
* @param longitude1
* 坐标1经度
* @param latitude1
* 坐标1纬度
* @param longitude2
* 坐标2经度
* @param latitude2
* 坐标1纬度
* @return
*/
private static double countDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
double radLat1 = Math.toRadians(latitude1);
double radLat2 = Math.toRadians(latitude2);
double a = radLat1 - radLat2;
double b = Math.toRadians(longitude1) - Math.toRadians(longitude2);
double s = 2 * Math.asin(Math.sqrt(
Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * 6378137.0;
s = Math.round(s * 10000) / 10000;
return s;
}
MySql计算两坐标点距离(单位:米):
SELECT
(2 * ATAN2(SQRT(SIN((
a.latitude - #{latitude}) * PI() / 180 / 2) * SIN((a.latitude - #{latitude}) * PI() / 180 / 2) + COS(#{latitude} * PI() / 180) * COS(a.latitude * PI() / 180) * SIN((a.longitude - #{longitude}) * PI() / 180 / 2) * SIN((a.longitude - #{longitude}) * PI() / 180 / 2)
),
SQRT(
1- SIN(
(
a.latitude - #{latitude}) * PI() / 180 / 2) * SIN((a.latitude - #{latitude}) * PI() / 180 / 2) + COS(#{latitude} * PI() / 180) * COS(a.latitude * PI() / 180) * SIN((a.longitude - #{longitude}) * PI() / 180 / 2) * SIN((a.longitude - #{longitude}) * PI() / 180 / 2)
)
)
) * 6378100 AS distance FROM 表 a