browser lang:en
Because of the near-spherical shape of the Earth, calculating an accurate distance between two points requires the use of spherical geometry [1], and trigonometric math functions. For many applications, an approximate distance calculation provides sufficient accuracy with much less complexity.
To calculate an approximate distance in miles, we could do:
v sqrt(x * x + y * y)
where:
x = 69.1 * (lat2 - lat1) and y = 53.0 * (lon2 - lon1)
We can improve the accuracy, by adding the cosine math function:
sqrt(x * x + y * y)
where:
x = 69.1 * (lat2 - lat1) and y = 69.1 * (lon2 - lon1) * cos(lat1/57.3)
If you need greater accuracy, you can use the Great Circle Distance Formula [2]. This formula requires use of spherical geometry, and a high level of floating point mathematical accuracy - about 15 digits of accuracy (double-precision).
To convert latitude or longitude from decimal degrees to radians, we can divide the latitude and longitude values by 180/pi, or approximately 57.29577951. The radius of the earth is assumed to be 6,378.8 kilometers, or 3,963.0 miles.
Since we are using PHP, it’s much simpler, because the deg2rad() function does this calculation for us.
If you convert all latitude and longitude values to radians before the calculation, we can use this equation:
3963.0 * arccos[sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1)] Implementing this in PHP
I implement the example with kilometers:
function getDistance($latitudeFrom, $longitudeFrom,
$latituteTo, $longitudeTo)
{
// 1 degree equals 0.017453292519943 radius
$degreeRadius = deg2rad(1);
// convert longitude and latitude values
// to radians before calculation
$latitudeFrom *= $degreeRadius;
$longitudeFrom *= $degreeRadius;
$latituteTo *= $degreeRadius;
$longitudeTo *= $degreeRadius;
// apply the Great Circle Distance Formula
$d = sin($latitudeFrom) * sin($latituteTo) + cos($latitudeFrom)
* cos($latituteTo) * cos($longitudeFrom - $longitudeTo);
return (6371.0 * acos($d));
}
if you want the final result to be in miles:
The radius of the earth is assumed to be 6,378.8 kilometers, or 3,963.0 miles, so you only need to change:
return (6371.0 * acos($d));
to:
return (3963.0 * acos($d));
Resources
[1]
[2]
Have you ever had to develop something yourself only to find out that there had already been…
in:JQuery (0 comments)The situation begins with your blog or website and you need to post some code on a particular…
in:The Holy Faq's (0 comments)So. Google just recently announced Google Buzz. I’m not sure about you, but I…
in:Blog (0 comments)There's lots of clever scripts around to tell you how to get images in and out of…
in:PHP scripts (0 comments)Spherical Law of Cosines Suppose that we want to find the five nearest places to (47.470779, -87.890699) using Spherical…
in:MySql (0 comments)I have always used Dreamweaver, and love it. But, I have been thinking, what are the FREE CSS…
in:Free Software (0 comments)Advanced Linux Sound Architecture (known by the acronym ALSA) is a Linux kernel component intended to replace the…
in:Blog (0 comments)The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a…
in:Scripts and tutorials (0 comments)
