示例#1
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);
 }
示例#2
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);
 }
示例#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;
 }
示例#4
0
文件: Geo.php 项目: speedwork/helpers
 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);
 }
示例#5
0
 /**
  * Create a default geocoder instance.
  * 
  * @return Geocoder
  */
 protected function getGeocoder()
 {
     $geocoder = new ProviderAggregator();
     $httpAdapter = new FopenHttpAdapter();
     $geocoder->registerProvider(new FreeGeoIp($httpAdapter));
     return $geocoder;
 }