/** * * @param GeoLocation $location * @return String the time zone identifier * * @throws ServiceError */ public function getTimeZone(GeoLocation $location) { $url = $this->formatUrl(self::DEFAULT_URL_TEMPLATE, ['lng' => $location->getLongitude(), 'lat' => $location->getLongitude(), 'apiKey' => $this->apiKey]); $response = json_decode($this->callUrl($url)); if ($response->status === self::STATUS_OK) { return $response->zoneName; } $this->lastError = new ServiceError(ServiceError::ERR_NOT_FOUND, $response->message); return null; }
public function distanceLookup(GeoLocation $source, GeoLocation $destination) { $data = array("sourceLatLong" => $source->getAddress() ?: strval($source), "destLatLong" => $destination->getAddress() ?: strval($destination), 'apiKey' => $this->apiKey); $result = $this->callUrl($this->formatUrl(self::URL_ROUTE, $data, false)); $resultDecoded = json_decode($result, true); if ($this->checkError($resultDecoded)) { return -1; } $distance = $resultDecoded['resourceSets'][0]['resources'][0]['travelDistance']; return $distance ? $distance * 1000 : -1; //return }
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; }