• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
59%
SHARE
TWEET

Haversine formula

a guest Mar 17th, 2012 410 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. <?php
2. /*
3.  * SQL Function:
4.  *
5.  * delimiter //
6.  * create function geoDistance(long1 decimal(14,10), lat1 decimal(14,10), long2 decimal(14,10), lat2 decimal(14,10)) returns decimal(10,2) BEGIN SET @long1 = long1*PI()/180; SET @lat1 = lat1*PI()/180; SET @long2 = long2*PI()/180; SET @lat2 = lat2*PI()/180; RETURN ACOS(SIN(@lat1) * SIN(@lat2) + COS(@lat1) * COS(@lat2) * COS(@long1 - @long2)) * 6371000; END; //
7.  * delimiter ;
8.  */
9.
10. function calculateDistance(\$lat_a,\$lat_b,\$long_a,\$long_b) {
11.         \$lat_a = \$lat_a * PI()/180;
12.         \$lat_b = \$lat_b * PI()/180;
13.         \$long_a = \$long_a * PI()/180;
14.         \$long_b = \$long_b * PI()/180;
15.         \$distance =
16.                 acos(
17.                         sin(\$lat_a ) * sin(\$lat_b) +
18.                         cos(\$lat_a) * cos(\$lat_b) * cos(\$long_b - \$long_a)
19.                 ) * 6371;
20.         return \$distance;
21. }
22. ?>
RAW Paste Data
Top