public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
double lat1 = Math.toRadians(latitude1);
double lat2 = Math.toRadians(latitude2);
double lng1 = Math.toRadians(longitude1);
double lng2 = Math.toRadians(longitude2);
double a = lat1 - lat2;
double b = lng1 - lng2;
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * EARTH_RADIUS;
if (0 >= s) {
return 0;
}
BigDecimal decimal = new BigDecimal(s).setScale(2, BigDecimal.ROUND_HALF_UP);
return decimal.doubleValue();
}
create
definer = web_user@`%` function get_distance(lng1 double, lat1 double, lng2 double, lat2 double) returns double
BEGIN
RETURN ROUND(
6378.138 * 2 * ASIN(
SQRT(
POW(SIN((lat1 * PI() / 180 - lat2 * PI() / 180) / 2), 2)
+ COS(lat1 * PI() / 180) * COS(lat2 * PI() / 180)
* POW(SIN((lng1 * PI() / 180 - lng2 * PI() / 180) / 2), 2)
)
) * 1000
);
END;