In PHP, I have the following code for calculating the distance between two locations:
Just came a cross this topic. Maybe someone will be interested in this function, tested in MySql 5.7:
create function GetDistance(lat1 real, lng1 real, lat2 real, lng2 real) returns real no sql
return ATAN2(SQRT(POW(COS(RADIANS(lat2)) * SIN(RADIANS(lng1 - lng2)), 2) +
POW(COS(RADIANS(lat1)) * SIN(RADIANS(lat2)) - SIN(RADIANS(lat1)) *
COS(RADIANS(lat2)) * COS(RADIANS(lng1 - lng2)), 2)),
(SIN(RADIANS(lat1)) * SIN(RADIANS(lat2)) + COS(RADIANS(lat1)) *
COS(RADIANS(lat2)) * COS(RADIANS(lng1 - lng2)))) * 6372.795;
Calling:
select GetDistance(your_latitude, your_longitude, table_field_lat,
table_field_lng) as dist from [your_table] limit 5;