Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you're looking for PHP code for calculating distance between two sets of coordinates here's a class that I adapted that will calculate the distance in kilometers. However, if you're using a database I would suggest that you look into whether or not your database is capable of spacial computations (I know that SQL Server and MySQL are, off the top of my head).</p> <p>Here's an interesting link for a SQL solution that you might want to check out. <a href="https://stackoverflow.com/questions/2226489/optimising-a-haversine-formula-sql-call-in-php">Optimising a haversine formula SQL call in PHP</a></p> <pre><code>class Distance { /** * Mean raidus of the earth in kilometers. * @var double */ const RADIUS = 6372.797; /** * Pi divided by 180 degrees. Calculated with PHP Pi constant. * @var double */ const PI180 = 0.017453293; /** * Constant for converting kilometers into miles. * @var double */ const MILES = 0.621371192; /** * Calculate distance between two points of latitude and longitude. * @param double $lat1 The first point of latitude. * @param double $long1 The first point of longitude. * @param double $lat2 The second point of latitude. * @param double $long2 The second point of longitude. * @param bool $kilometers Set to false to return in miles. * @return double The distance in kilometers or miles, whichever selected. */ public static function getDistance($lat1, $long1, $lat2, $long2, $kilometers = true) { $lat1 *= self::PI180; $long1 *= self::PI180; $lat2 *= self::PI180; $long2 *= self::PI180; $dlat = $lat2 - $lat1; $dlong = $long2 - $long1; $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlong / 2) * sin($dlong / 2); $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); $km = self::RADIUS * $c; if($kilometers) { return $km; } else { return $km * self::MILES; } } } //example echo Distance::getDistance(40.686748, -89.555054, 40.453078, -88.939819); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload