/**
  * @param null|String $ip if null will take ip from $_SERVER['REMOTE_ADDR']
  * @return array
  */
 public function getGeoByIp($ip = null)
 {
     if ($ip === null) {
         $ip = $_SERVER['REMOTE_ADDR'];
     }
     return $this->getResult(self::$geoCoder->geocode($ip));
 }
Example #2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $coordinate = new Coordinate($input->getArgument('coordinate'));
     $geocoder = new ProviderAggregator();
     $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()));
     }
     $batch = new Batch($geocoder);
     if ($input->getOption('cache')) {
         $cache = $this->getCache($input->getOption('cache'));
         $batch->setCache(new $cache());
     }
     $reversed = $batch->reverse($coordinate)->parallel();
     $address = $reversed[0]->getAddress();
     if ($input->getOption('raw')) {
         $result = array();
         $result[] = sprintf('<label>Adapter</label>:       <value>%s</value>', $adapter);
         $result[] = sprintf('<label>Provider</label>:      <value>%s</value>', $provider);
         $result[] = sprintf('<label>Cache</label>:         <value>%s</value>', isset($cache) ? $cache : 'None');
         if ($input->getOption('args')) {
             $result[] = sprintf('<label>Arguments</label>:     <value>%s</value>', $args);
         }
         $result[] = '---';
         $result[] = sprintf('<label>Latitude</label>:      <value>%s</value>', $address->getLatitude());
         $result[] = sprintf('<label>Longitude</label>:     <value>%s</value>', $address->getLongitude());
         if ($address->getBounds()->isDefined()) {
             $bounds = $address->getBounds()->toArray();
             $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>', $address->getStreetNumber());
         $result[] = sprintf('<label>Street Name</label>:   <value>%s</value>', $address->getStreetName());
         $result[] = sprintf('<label>Zipcode</label>:       <value>%s</value>', $address->getPostalCode());
         $result[] = sprintf('<label>City</label>:          <value>%s</value>', $address->getLocality());
         $result[] = sprintf('<label>City District</label>: <value>%s</value>', $address->getSubLocality());
         if (NULL !== ($adminLevels = $address->getAdminLevels())) {
             $result[] = '<label>Admin Levels</label>';
             foreach ($adminLevels as $adminLevel) {
                 $result[] = sprintf(' - <label>%s</label>: <value>%s</value>', $adminLevel->getCode(), $adminLevel->getName());
             }
         }
         $result[] = sprintf('<label>Country</label>:       <value>%s</value>', $address->getCountry()->toString());
         $result[] = sprintf('<label>Country Code</label>:  <value>%s</value>', $address->getCountryCode());
         $result[] = sprintf('<label>Timezone</label>:      <value>%s</value>', $address->getTimezone());
     } elseif ($input->getOption('json')) {
         $result = sprintf('<value>%s</value>', json_encode($address->toArray()));
     } else {
         $result = (new StringFormatter())->format($address, $input->getOption('format'));
     }
     $output->writeln($result);
 }
Example #3
0
 /**
  * Constructor. Set the geocoder instance.
  *
  * @param null|Geocoder $geocoder
  */
 public function __construct(Geocoder $geocoder = null)
 {
     if ($geocoder === null) {
         $geocoder = new ProviderAggregator();
         $geocoder->registerProvider(new FreeGeoIp(new FopenHttpAdapter()));
     }
     $this->geocoder = $geocoder;
 }
Example #4
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $geocoder = new ProviderAggregator();
     $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'));
     $geocoded = $geocoded->first();
     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()->toArray())) {
             $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->getPostalCode());
         $result[] = sprintf('<label>City</label>:          <value>%s</value>', $geocoded->getLocality());
         $result[] = sprintf('<label>City District</label>: <value>%s</value>', $geocoded->getSublocality());
         if (NULL !== ($adminLevels = $geocoded->getAdminLevels())) {
             $result[] = '<label>Admin Levels</label>';
             foreach ($adminLevels as $adminLevel) {
                 $result[] = sprintf(' - <label>%s</label>: <value>%s</value>', $adminLevel->getCode(), $adminLevel->getName());
             }
         }
         $result[] = sprintf('<label>Country</label>:       <value>%s</value>', $geocoded->getCountry()->toString());
         $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);
 }
 /**
  * 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['geocoder'] = $this->app->share(function ($app) {
         $geocoder = new ProviderAggregator();
         $geocoder->registerProviders($this->getGeocoderProviders($this->app['config']->get('geocoder.providers')));
         return $geocoder;
     });
 }
 /**
  * Create a default geocoder instance.
  * 
  * @return Geocoder
  */
 private function getGeocoder()
 {
     $geocoder = new ProviderAggregator();
     $httpAdapter = new FopenHttpAdapter();
     $geocoder->registerProvider(new FreeGeoIp($httpAdapter));
     return $geocoder;
 }
 /**
  * {@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;
 }
Example #8
0
 public function get($address = null, $db = 'GeoLite2-City.mmdb')
 {
     if ($address === null) {
         $address = ip();
     }
     //http://ipinfo.io/119.63.142.37/json
     $geocoder = new ProviderAggregator();
     $adapter = new CurlHttpAdapter();
     $reader = new Reader(STORAGE . '/' . $db);
     $geoIP2Adapter = new GeoIP2Adapter($reader);
     $chain = new Chain([new GeoIP2($geoIP2Adapter), new FreeGeoIp($adapter), new HostIp($adapter), new GoogleMaps($adapter)]);
     $geocoder->registerProvider($chain);
     $results = false;
     try {
         $results = $geocoder->geocode($address);
     } catch (\Exception $e) {
         $results = false;
     }
     $result = [];
     if ($results !== false) {
         foreach ($results as $value) {
             try {
                 $region = $value->getAdminLevels()->get(1);
             } catch (\Exception $e) {
                 $region = false;
             }
             $result['latitude'] = $value->getLatitude();
             $result['longitude'] = $value->getLongitude();
             $result['country'] = $value->getCountry()->getName();
             $result['country_code'] = $value->getCountryCode();
             $result['city'] = $value->getLocality();
             $result['region'] = $region ? $region->getName() : '';
             $result['region_code'] = $region ? $region->getCode() : '';
             $result['zipcode'] = $value->getPostalCode();
             $result['locality'] = $value->getSubLocality();
         }
     }
     return array_map('trim', $result);
 }
Example #9
0
 /**
  * {@inheritDoc}
  */
 public function getName()
 {
     return $this->provider->getName() . '_best_match';
 }
Example #10
0
 /**
  * {@inheritDoc}
  */
 public function reverse($latitude, $longitude)
 {
     $reversed = $this->aggregator->reverse($latitude, $longitude);
     return new AddressCollection($reversed->slice(0, $this->getLimit()));
 }
 /**
  * @param float
  * @param float
  * @return ProviderAndDumperAggregator
  */
 public function reverse($latitude, $longitude)
 {
     $this->results = parent::reverse($latitude, $longitude);
     return $this;
 }
 public function getProvider()
 {
     $this->countCallGetProvider++;
     return parent::getProvider();
 }
Example #13
0
 private function getLocation()
 {
     $request = Request::createFromGlobals();
     $adapter = new CurlHttpAdapter();
     $geocoder = new ProviderAggregator();
     $geocoder->registerProviders([new FreeGeoIp($adapter), new GeoPlugin($adapter)]);
     $location = 'n/a';
     try {
         $result = $geocoder->geocode($request->getClientIp());
         if ($result->count()) {
             $address = $result->first();
             $state = $address->getAdminLevels()->first();
             $state = $state !== null ? $state->getName() : '';
             $parts = new ArrayObject([$address->getCountry()->getName(), $state, $address->getLocality()]);
             $parts = $parts->filter(function ($elem) {
                 return !empty($elem);
             });
             $loc = $parts->join(', ')->trim(' ,')->toString();
             if (!empty($loc)) {
                 $location = $loc;
             }
         }
     } catch (\Exception $e) {
     }
     return $location;
 }