public function distanceLookup(GeoLocation $source, GeoLocation $destination) { $data = array('from' => urlencode($source->getLatitude() && $source->getLongitude() ? strval($source) : $source->getAddress()), 'to' => urlencode($destination->getLatitude() && $destination->getLongitude() ? strval($destination) : $destination->getAddress()), 'apiKey' => $this->apiKey); $url = $this->formatUrl(self::URL_DISTANCE_LOOKUP, $data, true); $apiResult = json_decode($this->callUrl($url), true); if ($this->hasError($apiResult, false)) { return null; } $totalDist = 0; if (count($apiResult['route']['legs'])) { foreach ($apiResult['route']['legs'] as $leg) { $totalDist += $leg['distance']; } $totalDist = $totalDist * 1000; //cast to meters } else { $totalDist = -1; } return $totalDist; }
public static function haversineGreatCircleDistance(GeoLocation $from, GeoLocation $to) { // convert from degrees to radians $latFrom = deg2rad($from->getLatitude()); $lonFrom = deg2rad($from->getLongitude()); $latTo = deg2rad($to->getLatitude()); $lonTo = deg2rad($to->getLongitude()); $latDelta = $latTo - $latFrom; $lonDelta = $lonTo - $lonFrom; $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) + cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2))); return $angle * self::EARTH_RADIUS; }