This is an example of how to calculate the distance between two GPS coordinates as a function in MySQL.
For calculating the distance of the two given GPS coordinates, we use the Harversine formula.
In order to use the distance function, just type
select DISTANCE(24.784734,120.995724,24.785498,120.996633,'KM');
which will return a table with one entry with the requested value.
This function can take the following measure of units as inputs:
- Kilometers: Type 'KILOMETER' or 'KM' as input.
- Miles: Type 'MILE' or 'MI' as input.
If just want to measure the distace between two GPS coordinates, use a SELECT statement.
select DISTANCE(24.784734,120.995724,24.785498,120.996633,'KM');
select DISTANCE(24.784734,120.995724,24.785498,120.996633,'MI');
Because the function returns a unique number, it can be used inside functions and stored procedures as follows
if distance(user1_gps_latitude,users1_gps_longitude,user2_gps_latitude,users2_gps_longitude,'KM')<0.09 then
# Insert your code here
end if;