Author: William Durand (william.durand1@gmail.com)
Inheritance: implements geocoder\GeocoderInterface
 public function geolocateSubmit()
 {
     if (isset($_POST['lat'], $_POST['lng'])) {
         $lat = $_POST['lat'];
         $lng = $_POST['lng'];
         $url = sprintf("https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s", $lat, $lng);
         $content = file_get_contents($url);
         // get json content
         $metadata = json_decode($content, true);
         //json decoder
         if (count($metadata['results']) > 0) {
             // for format example look at url
             // https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452
             $result = $metadata['results'][0];
             $GeoJSON = new \Geocoder\Dumper\GeoJsonDumper();
             $adapter = new \Geocoder\HttpAdapter\CurlHttpAdapter();
             $geocoder = new \Geocoder\Geocoder();
             $geocoder->registerProvider(new \Geocoder\Provider\GoogleMapsProvider($adapter));
             $address = $geocoder->reverse($lat, $lng);
             $result = $this->locationFormatter($address);
             return view('result', $result);
         } else {
             // no results returned
         }
     }
 }
 /**
  * Register the service provider.
  *
  * @return void
  */
 public function register()
 {
     $this->app->singleton('geocoder.adapter', function ($app) {
         $adapter = $app['config']->get('geocoder.adapter');
         return new $adapter();
     });
     $this->app->singleton('geocoder.chain', function ($app) {
         $providers = [];
         foreach ($app['config']->get('geocoder.providers') as $provider => $arguments) {
             if (0 !== count($arguments)) {
                 $providers[] = call_user_func_array(function ($arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null) use($app, $provider) {
                     return new $provider($app['geocoder.adapter'], $arg1, $arg2, $arg3, $arg4);
                 }, $arguments);
                 continue;
             }
             $providers[] = new $provider($app['geocoder.adapter']);
         }
         return new ChainProvider($providers);
     });
     $this->app['geocoder'] = $this->app->share(function ($app) {
         $geocoder = new Geocoder();
         $geocoder->registerProvider($app['geocoder.chain']);
         return $geocoder;
     });
 }
Exemplo n.º 3
0
 /**
  * Get address from requested param
  *
  * @param string $param
  * @throws \Sonar\Exceptions\GeoServiceException
  * @return array
  */
 public function location($param)
 {
     try {
         $result = $this->geocoder->geocode($param);
         // parse  data from geo locator
         return (new Geo($result))->toArray();
     } catch (\Exception $e) {
         throw new GeoServiceException($e->getMessage());
     }
 }
Exemplo n.º 4
0
 /**
  * Execute the middleware.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  * @param callable               $next
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     if (!Middleware::hasAttribute($request, ClientIp::KEY)) {
         throw new RuntimeException('Geolocate middleware needs ClientIp executed before');
     }
     $ip = ClientIp::getIp($request);
     if ($ip !== null) {
         $request = Middleware::setAttribute($request, self::KEY, $this->geocoder->geocode($ip));
     }
     return $next($request, $response);
 }
Exemplo n.º 5
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $geocoder = new Geocoder();
     $adapter = $this->getAdapter($input->getOption('adapter'));
     $provider = $this->getProvider($input->getOption('provider'));
     if ($input->getOption('args')) {
         $args = is_array($input->getOption('args')) ? implode(',', $input->getOption('args')) : $input->getOption('args');
         $geocoder->registerProvider(new $provider(new $adapter(), $args));
     } else {
         $geocoder->registerProvider(new $provider(new $adapter()));
     }
     $geocoded = $geocoder->geocode($input->getArgument('value'));
     if ($input->getOption('raw')) {
         $result = array();
         $result[] = sprintf('<label>Adapter</label>:       <value>%s</value>', $adapter);
         $result[] = sprintf('<label>Provider</label>:      <value>%s</value>', $provider);
         if ($input->getOption('args')) {
             $result[] = sprintf('<label>Arguments</label>:     <value>%s</value>', $args);
         }
         $result[] = '---';
         $result[] = sprintf('<label>Latitude</label>:      <value>%s</value>', $geocoded->getLatitude());
         $result[] = sprintf('<label>Longitude</label>:     <value>%s</value>', $geocoded->getLongitude());
         if (null !== ($bounds = $geocoded->getBounds())) {
             $result[] = '<label>Bounds</label>';
             $result[] = sprintf(' - <label>South</label>: <value>%s</value>', $bounds['south']);
             $result[] = sprintf(' - <label>West</label>:  <value>%s</value>', $bounds['west']);
             $result[] = sprintf(' - <label>North</label>: <value>%s</value>', $bounds['north']);
             $result[] = sprintf(' - <label>East</label>:  <value>%s</value>', $bounds['east']);
         }
         $result[] = sprintf('<label>Street Number</label>: <value>%s</value>', $geocoded->getStreetNumber());
         $result[] = sprintf('<label>Street Name</label>:   <value>%s</value>', $geocoded->getStreetName());
         $result[] = sprintf('<label>Zipcode</label>:       <value>%s</value>', $geocoded->getZipcode());
         $result[] = sprintf('<label>City</label>:          <value>%s</value>', $geocoded->getCity());
         $result[] = sprintf('<label>City District</label>: <value>%s</value>', $geocoded->getCityDistrict());
         $result[] = sprintf('<label>County</label>:        <value>%s</value>', $geocoded->getCounty());
         $result[] = sprintf('<label>County Code</label>:   <value>%s</value>', $geocoded->getCountyCode());
         $result[] = sprintf('<label>Region</label>:        <value>%s</value>', $geocoded->getRegion());
         $result[] = sprintf('<label>Region Code</label>:   <value>%s</value>', $geocoded->getRegionCode());
         $result[] = sprintf('<label>Country</label>:       <value>%s</value>', $geocoded->getCountry());
         $result[] = sprintf('<label>Country Code</label>:  <value>%s</value>', $geocoded->getCountryCode());
         $result[] = sprintf('<label>Timezone</label>:      <value>%s</value>', $geocoded->getTimezone());
     } elseif ($input->getOption('json')) {
         $result = sprintf('<value>%s</value>', json_encode($geocoded->toArray()));
     } elseif ($input->getOption('dumper')) {
         $dumper = $this->getDumper($input->getOption('dumper'));
         $dumper = new $dumper();
         $result = sprintf('<value>%s</value>', $dumper->dump($geocoded));
     } else {
         $result = sprintf('<value>%s, %s</value>', $geocoded->getLatitude(), $geocoded->getLongitude());
     }
     $output->writeln($result);
 }
Exemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function reverse($latitude, $longitude)
 {
     if ($this->getProvider() instanceof GeocoderProvider) {
         return $this->getProvider()->getReversedData(array($latitude, $longitude));
     }
     return parent::reverse($latitude, $longitude);
 }
 protected function setUp()
 {
     AnnotationRegistry::registerLoader('class_exists');
     $conn = DriverManager::getConnection(array('driver' => 'pdo_sqlite', 'memory' => true));
     $this->em = $this->_getTestEntityManager($conn);
     $reader = new SimpleAnnotationReader();
     $reader->addNamespace('Bazinga\\Bundle\\GeocoderBundle\\Mapping\\Annotations');
     $reader->addNamespace('Doctrine\\ORM\\Mapping');
     $driver = new AnnotationDriver($reader);
     $geocoder = new Geocoder();
     $geocoder->registerProvider(new GoogleMapsProvider(new CurlHttpAdapter()));
     $this->listener = new GeocoderListener($geocoder, $driver);
     $this->em->getEventManager()->addEventSubscriber($this->listener);
     $sm = new SchemaTool($this->em);
     $sm->createSchema(array($this->em->getClassMetadata('Bazinga\\Bundle\\GeocoderBundle\\Tests\\Doctrine\\ORM\\Dummy')));
 }
Exemplo n.º 8
0
 /**
  * Constructs a geocoder and builds providers from plugin settings
  */
 function __construct()
 {
     if (!isset(self::$adapter)) {
         self::$adapter = new GuzzleHttpAdapter();
     }
     if (!isset(self::$providers)) {
         self::$providers = array_filter(array($this->buildFreeGeoIpProvider()));
     }
     if (!count(self::$providers)) {
         elgg_add_admin_notice('geo:providers', elgg_echo('geo:providers:none'));
     } else {
         if (!isset(self::$geocoder)) {
             $geocoder = new Geocoder();
             $geocoder->registerProviders(self::$providers);
             self::$geocoder = $geocoder;
         }
     }
 }
Exemplo n.º 9
0
 public function compare(\Geocoder\Geocoder $geocoder)
 {
     $data = new \stdClass();
     try {
         $time_pre = microtime(true);
         $address = $geocoder->limit(1)->geocode($this->query)->first();
         $time_post = microtime(true);
         $formatter = new \Geocoder\Formatter\StringFormatter();
         $data->address = $formatter->format($address, "%S %n, %z %L");
         $data->latitude = $address->getLatitude();
         $data->longitude = $address->getLongitude();
         $data->microtime = $time_post - $time_pre;
     } catch (\Exception $e) {
         $data = $e->getMessage();
     }
     $this->table[$geocoder->getName()] = $data;
     return $this;
 }
 /**
  * {@inheritDoc}
  */
 public function reverse($latitude, $longitude)
 {
     if ($this->getProvider() instanceof Provider) {
         $value = $latitude . '-' . $longitude;
         return $this->getProvider()->getReversedData(array($latitude, $longitude));
     } else {
         return parent::reverse($latitude, $longitude);
     }
 }
Exemplo n.º 11
0
 /**
  * Constructs a geocoder and builds providers from plugin settings
  */
 function __construct()
 {
     if (!isset(self::$adapter)) {
         self::$adapter = new CurlHttpAdapter();
     }
     if (!isset(self::$providers)) {
         self::$providers = array_filter(array($this->buildGoogleMapsProvider(), $this->buildNominatimProvider(), $this->buildYandexProvider(), $this->buildGoogleMapsBusinessProvider()));
     }
     if (!count(self::$providers)) {
         elgg_add_admin_notice('geo:providers', elgg_echo('geo:providers:none'));
     } else {
         if (!isset(self::$geocoder)) {
             $geocoder = new Geocoder();
             $chain = new ChainProvider(self::$providers);
             $geocoder->registerProvider($chain);
             self::$geocoder = $geocoder;
         }
     }
 }
Exemplo n.º 12
0
 protected function tryToGetAddressFromGeocode($address, $attempts = 0)
 {
     if ($attempts >= 3) {
         return;
     }
     $result = null;
     /**
      * @var \Geocoder\Model\Address
      */
     try {
         $result = $this->geocoder->geocode($address)->first();
         return $result;
     } catch (\Geocoder\Exception\NoResult $no_result) {
         $this->writeString('x');
         return;
     } catch (\ErrorException $response_error) {
         $this->tryToGetAddressFromGeocode($address, ++$attempts);
     }
     return $result;
 }
 /**
  * {@inheritDoc}
  */
 public function reverse($latitude, $longitude)
 {
     if (null === $this->logger) {
         return parent::reverse($latitude, $longitude);
     }
     $startTime = microtime(true);
     $results = parent::reverse($latitude, $longitude);
     $duration = (microtime(true) - $startTime) * 1000;
     $value = sprintf("[Reverse geocoding] latitude: %s, longitude: %s", $latitude, $longitude);
     $this->logger->logRequest($value, $duration, $this->getProviderClass(), $results);
     return $results;
 }
Exemplo n.º 14
0
 /**
  * Execute the middleware.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  * @param callable               $next
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     if (!self::hasAttribute($request, ClientIp::KEY)) {
         throw new RuntimeException('Geolocate middleware needs ClientIp executed before');
     }
     $ip = ClientIp::getIp($request);
     if ($ip !== null) {
         if ($this->saveInSession) {
             $ips =& self::getStorage($request, self::KEY);
             if (isset($ips[$ip])) {
                 $address = new AddressCollection($ips[$ip]);
             } else {
                 $address = $this->geocoder->geocode($ip);
                 $ips[$ip] = $address->all();
             }
         } else {
             $address = $this->geocoder->geocode($ip);
         }
         $request = self::setAttribute($request, self::KEY, $address);
     }
     return $next($request, $response);
 }
Exemplo n.º 15
0
 /**
  * @param null $useChain
  *
  * @return Geocoder
  */
 protected function instance($useChain = NULL)
 {
     if (!isset($this->_geocoder) or NULL !== $useChain) {
         // Initializing Geocoder
         $this->_geocoder = new \Geocoder\Geocoder();
         if ($useChain && count($this->_providers) > 1) {
             $this->_geocoder->registerProvider(new \Geocoder\Provider\ChainProvider($this->_providers));
         } else {
             $this->_geocoder->registerProviders($this->_providers);
         }
     }
     return $this->_geocoder;
 }
Exemplo n.º 16
0
 public function getProvider()
 {
     $this->countCallGetProvider++;
     return parent::getProvider();
 }
Exemplo n.º 17
0
 /**
  * Returns registered providers indexed by name.
  *
  * @return \Geocoder\ProviderInterface[] 
  * @static 
  */
 public static function getProviders()
 {
     return \Geocoder\Geocoder::getProviders();
 }
Exemplo n.º 18
0
 protected function geocodeAddress()
 {
     $this->geocoderResult = $this->geocoder->geocode($this->address);
     return true;
 }
Exemplo n.º 19
0
 public function returnResult(array $data = array())
 {
     return parent::returnResult($data);
 }
Exemplo n.º 20
0
 function it_throws_exception_if_country_is_not_geolocated(Geocoder $geocoder, Money $money)
 {
     $geocoder->geocode(Argument::any())->shouldBeCalled()->willReturn(new AddressCollection());
     $this->shouldThrow('Locurro\\Exception\\GeolocationException')->duringConvert($money, 'umpirsky');
 }
Exemplo n.º 21
0
 /**
  * Get geo mockup data
  *
  * @return \Geocoder\Model\AddressCollection
  */
 public function getMockData()
 {
     return $this->geocoder->geocode(self::IP_ADDRESS);
 }