/** * Looks up the geo coordinates of the address of an object and sets its * geo coordinates. * * @param tx_oelib_Interface_Geo $geoObject * the object for which the geo coordinates will be looked up and set * * @return void */ public function lookUp(tx_oelib_Interface_Geo $geoObject) { if ($geoObject->hasGeoError() || $geoObject->hasGeoCoordinates()) { return; } if (!$geoObject->hasGeoAddress()) { $geoObject->setGeoError(); return; } if (!empty($this->coordinates)) { $geoObject->setGeoCoordinates($this->coordinates); } else { $geoObject->setGeoError(); } }
/** * Calculates the great-circle distance in kilometers between two geo * objects using the haversine formula. * * @param tx_oelib_Interface_Geo $object1 * the first object, must have geo coordinates * @param tx_oelib_Interface_Geo $object2 * the second object, must have geo coordinates * * @return float the distance between $object1 and $object2 in kilometers, will be >= 0.0 * * @throws InvalidArgumentException */ public function calculateDistanceInKilometers(tx_oelib_Interface_Geo $object1, tx_oelib_Interface_Geo $object2) { if ($object1->hasGeoError()) { throw new InvalidArgumentException('$object1 has a geo error.'); } if ($object2->hasGeoError()) { throw new InvalidArgumentException('$object2 has a geo error.'); } if (!$object1->hasGeoCoordinates()) { throw new InvalidArgumentException('$object1 needs to have coordinates, but has none.'); } if (!$object2->hasGeoCoordinates()) { throw new InvalidArgumentException('$object2 needs to have coordinates, but has none.'); } $coordinates1 = $object1->getGeoCoordinates(); $latitude1 = deg2rad($coordinates1['latitude']); $longitude1 = deg2rad($coordinates1['longitude']); $coordinates2 = $object2->getGeoCoordinates(); $latitude2 = deg2rad($coordinates2['latitude']); $longitude2 = deg2rad($coordinates2['longitude']); return acos(sin($latitude1) * sin($latitude2) + cos($latitude1) * cos($latitude2) * cos($longitude2 - $longitude1)) * self::EARTH_RADIUS_IN_KILOMETERS; }
/** * Looks up the geo coordinates of the address of an object and sets its * geo coordinates. * * @param tx_oelib_Interface_Geo $geoObject * the object for which the geo coordinates will be looked up and set * * @return void */ public function lookUp(tx_oelib_Interface_Geo $geoObject) { if ($geoObject->hasGeoError() || $geoObject->hasGeoCoordinates()) { return; } if (!$geoObject->hasGeoAddress()) { $geoObject->setGeoError(); return; } $address = $geoObject->getGeoAddress(); $this->throttle(); $rawResult = $this->sendRequest($address); if ($rawResult === FALSE) { throw new RuntimeException('There was an error connecting to the Google geocoding server.', 1331488446); } $resultParts = json_decode($rawResult, TRUE); $status = $resultParts['status']; if ($status === self::STATUS_OK) { $coordinates = $resultParts['results'][0]['geometry']['location']; $geoObject->setGeoCoordinates(array('latitude' => (double) $coordinates['lat'], 'longitude' => (double) $coordinates['lng'])); } else { $geoObject->setGeoError(); } }