Skip to content

Instantly share code, notes, and snippets.

@andrekeher
Created April 23, 2018 15:17
Show Gist options
  • Save andrekeher/f758f727e500b78bec777c02b12fae6c to your computer and use it in GitHub Desktop.
Save andrekeher/f758f727e500b78bec777c02b12fae6c to your computer and use it in GitHub Desktop.
<?php
/**
* Based on Moodle 3.x Developer's Guide book example
*/
class Haversine
{
public $radius = 6378100; // Meters
public function get_distance($x1, $y1, $x2, $y2)
{
$lon_arc = deg2rad(($x1 - $x2));
$lat_arc = deg2rad(($y1 - $y2));
$lonh = sin($lon_arc * 0.5);
$lonh *= $lonh;
$lath = sin($lat_arc * 0.5);
$lath *= $lath;
$tmp = cos(deg2rad($y1)) * cos(deg2rad($y2));
$distance = 2 * $this->radius * asin(sqrt($lath + $tmp * $lonh));
return $distance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment