In google maps, you can actually know the distance between location one and location two. This one makes it easier to plan before we undertake the activity as we are usually provided with a clue of what to expect to cover if you want to visit the place from a given location. The way Google does this is also used by taxi applications so that their pay is dependent on the distance covered and some other factors.
As we all know, places around the earth are identified by latitude and longitude which are called coordinates. Latitude is the measurement of distance north or south of the Equator. Below the equator, the latitude values will begin with a negative or minus sign (-). Longitude is the measurement east or west of the prime meridian. The longitude values west of the prime meridian usually have the minus sign while defining them.
An example of a coordinate with below values
Latitude -27.3890
Longitude 1.90765
Will be written as
(-27.3890, 1.90765)
In this article, I will explain how to calculate the distance between two latitudes and longitudes regardless of where they are. We will use SQL to calculate the distance which we will display in kilometers (kms).
First we need to have a table in the database that is storing the defined destination. Suppose we have a table called cities. I will have two entries of two places which I will calculate its distance from my starting point.
>>CityID | CityName | Latitude | Longitude |
---|---|---|---|
1 | City1 | 72.8777 | 19.0760 |
2 | City2 | 18.4241 | -33.9249 |
My coordinates which I will calculate distance from,
(36.8219,-1.2921)
To begin, we will need to define the radius of the earth which is known to be 6371
Next, we will use the SQL code below to calculate the distance as shown
SELECT ROUND(( 6371 * acos( cos( radians(36.8219) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians(-1.2921) ) + sin( radians(36.8219) ) * sin( radians( Latitude ) ) ) ),2) AS calculated_distance FROM cities
The above code snippet will return the distance between my location and the two cities that I have defined with 2 decimal places.
To include the city name in your results add the cityName column in your SQL query as follows
SELECT CityID,CityName, ROUND(( 6371 * acos( cos( radians(36.8219) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians(-1.2921) ) + sin( radians(36.8219) ) * sin( radians( Latitude ) ) ) ),2) AS calculated_distance FROM cities
To sort the distance from closest to longest add the ORDER BY keyword to the SQL query
SELECT CityID,CityName, ROUND(( 6371 * acos( cos( radians(36.8219) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians(-1.2921) ) + sin( radians(36.8219) ) * sin( radians( Latitude ) ) ) ),2) AS calculated_distance FROM cities ORDER BY calculated_distance ASC
To do the same in PHP, you will have to collect and define the variables first and then replace the variables in the SQL query as shown below
<?php
$latitude="36.8219";
$longitude="-1.2921";
SELECT CityID,CityName, ROUND(( 6371 * acos( cos( radians($latitude) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( Latitude ) ) ) ),2) AS calculated_distance FROM cities ORDER BY calculated_distance ASC;
We have shown the steps and SQL and PHP code to calculate the distance between two coordinates given the start and destination. You can use the above snippet in your application with ease and it can fit in any development language without a lot of modification.