Great Circle Distance

I need a MYSQL function which receive two geo-coordinates (standard latitude and longitude expressed as DECIMAL(12,9)) and must return the distance in miles between the two points

As far as i have seen these links.

http://www.experts-exchange.com/Database/MySQL/Q_23080406.html?sfQueryTermInfo=1+circl+distanc+great+mysql

http://www.meridianworlddata.com/Distance-Calculation.asp

LVL 23
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DBACommented:
Hi itkamaraj,

I don't believe that and of the major DMBS engines have a distance based on coordinates function built into them so you'll have to build one yourself.

Below is a PHP function to do that.  It will return kilometers or miles.  You'll just have to convert it to SQL.  :)

Good Luck,
Kent

``````function distance(\$lat1, \$lng1, \$lat2, \$lng2, \$miles = true)
{
\$pi80 = M_PI / 180;
\$lat1 *= \$pi80;
\$lng1 *= \$pi80;
\$lat2 *= \$pi80;
\$lng2 *= \$pi80;
\$r = 6372.797; // mean radius of Earth in km
\$dlat = \$lat2 - \$lat1;
\$dlng = \$lng2 - \$lng1;
\$a = sin(\$dlat / 2) * sin(\$dlat / 2) + cos(\$lat1) * cos(\$lat2) * sin(\$dlng / 2) * sin(\$dlng / 2);
\$c = 2 * atan2(sqrt(\$a), sqrt(1 - \$a));
\$km = \$r * \$c;
return (\$miles ? (\$km * 0.621371192) : \$km);
}
``````
0

Experts Exchange Solution brought to you by