After researching a lot and not coming to a solution i have to ask. How can I calculate the distance between two Long/Lat Points in Mysql? (efficient way?)
I also found this solution, but there I dont know which @lat @long is and i am not using Points because spring-data-jpa doesnt allow a Geometry Type (Except using MysqlSpatial dialect)
In Java i am calculating like this:
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
}
And my table/Entity contains one column for longitude and another for latitude.
Another question, could there be a possibility to solve this with as said before with spring data jpa?
I tried to translate the Java code to Mysql, but the Distances are to high.
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