1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
<?php
namespace AppBundle\services\Geocoder;
use Geocoder\Geocoder;
use Geocoder\GeocoderInterface;
use Geocoder\Provider\ProviderInterface;
use Symfony\Component\HttpFoundation\Request;
class UserGeocoder {
protected $geocoder;
protected $providers = [];
protected $user_ip;
public function __construct(Request $request, GeocoderInterface $geocoder) {
$this->geocoder = $geocoder;
$this->user_ip = $request->getClientIp();
if ($this->user_ip == '127.0.0.1') {
$this->user_ip = '64.15.116.91'; // google.fr
}
}
public function addGeocoder(ProviderInterface $provider) {
$this->providers[] = $provider;
}
public function getGeocoder() {
$res = null;
foreach ($this->providers as $provider) {
$this->geocoder->registerProvider($provider);
$res = $this->geocoder->geocode($this->user_ip);
if ($res->getCounty()!=null)
break;
}
return $res;
}
} |
Partager