コード例 #1
0
ファイル: Geocoder.php プロジェクト: shinesoftware/GoogleMaps
 /**
  * Execute geocoding
  * 
  * @param  Request $request
  * @return Response
  */
 public function geocode(Request $request)
 {
     if (null === $request) {
         throw new Exception\InvalidArgumentException('request');
     }
     $uri = new Uri();
     $uri->setHost(self::GOOGLE_MAPS_APIS_URL);
     $uri->setPath(self::GOOGLE_GEOCODING_API_PATH);
     $urlParameters = $request->getUrlParameters();
     if (null === $urlParameters) {
         throw new Exception\RuntimeException('Invalid URL parameters');
     }
     $uri->setQuery($urlParameters);
     $client = $this->getHttpClient();
     $client->setAdapter('Zend\\Http\\Client\\Adapter\\Curl');
     $client->resetParameters();
     $uri->setScheme("https");
     $client->setUri($uri->toString());
     $stream = $client->send();
     $body = Json::decode($stream->getBody(), Json::TYPE_ARRAY);
     $hydrator = new ArraySerializable();
     $response = new Response();
     $response->setRawBody($body);
     if (!isset($body['status'])) {
         throw new Exception\RuntimeException('Invalid status');
     }
     $response->setStatus($body['status']);
     if (!isset($body['results'])) {
         throw new Exception\RuntimeException('Invalid results');
     }
     $resultSet = new ResultSet();
     foreach ($body['results'] as $data) {
         $result = new Result();
         $hydrator->hydrate($data, $result);
         $resultSet->addElement($result);
     }
     $response->setResults($resultSet);
     return $response;
 }
コード例 #2
0
ファイル: AddressService.php プロジェクト: eotg1910/shineisp2
 /**
  * @inheritDoc
  */
 public function save(\Customer\Entity\Address $record)
 {
     $hydrator = new ClassMethods(true);
     $country = null;
     // get the country name
     if ($record->getCountryId()) {
         $country = $this->countryService->find($record->getCountryId());
         $strCountry = $country->getName();
     }
     // prepare the address string
     $strAddress = $record->getStreet() . " " . $record->getCode() . " " . $record->getCity() . " " . $strCountry;
     // get the data by Google Maps
     $request = new \GoogleMaps\Request();
     $request->setAddress($strAddress);
     $proxy = new \GoogleMaps\Geocoder();
     $response = $proxy->geocode($request);
     $results = $response->getResults();
     if (isset($results[0])) {
         $geometry = $results[0]->getGeometry()->getLocation();
         $record->setLatitude($geometry->getLat());
         $record->setLongitude($geometry->getLng());
     }
     // extract the data from the object
     $data = $hydrator->extract($record);
     $id = (int) $record->getId();
     $this->getEventManager()->trigger(__FUNCTION__ . '.pre', null, array('data' => $data));
     // Trigger an event
     if ($id == 0) {
         unset($data['id']);
         $this->tableGateway->insert($data);
         // add the record
         $id = $this->tableGateway->getLastInsertValue();
     } else {
         $rs = $this->find($id);
         if (!empty($rs)) {
             $this->tableGateway->update($data, array('id' => $id));
         } else {
             throw new \Exception('Record ID does not exist');
         }
     }
     $record = $this->find($id);
     $this->getEventManager()->trigger(__FUNCTION__ . '.post', null, array('id' => $id, 'data' => $data, 'record' => $record));
     // Trigger an event
     return $record;
 }