Want more features on Pastebin? Sign Up, it's FREE!
Guest

Haversine formula

By: a guest on Mar 17th, 2012  |  syntax: PHP  |  size: 0.86 KB  |  views: 379  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. ?>
clone this paste RAW Paste Data