├── .gitignore ├── README.md ├── HappyRGoogleGeocoderBundle.php ├── Tests └── Services │ └── ServiceTest.php ├── Resources └── config │ └── services.yml ├── .travis.yml ├── composer.json ├── DependencyInjection ├── Configuration.php └── HappyRGoogleGeocoderExtension.php ├── Services ├── ScraperService.php ├── PlacesAutocompleteService.php └── GeocodeService.php └── phpunit.xml.dist /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store 3 | build 4 | phpunit.xml 5 | composer.lock 6 | vendor 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GoogleMapsGeocoderBundle 2 | ===================== 3 | 4 | This bundle in deprecated. You should use the much more awesome [BazingaGeocoderBundle](https://github.com/geocoder-php/BazingaGeocoderBundle) 5 | -------------------------------------------------------------------------------- /HappyRGoogleGeocoderBundle.php: -------------------------------------------------------------------------------- 1 | assertTrue(true); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Resources/config/services.yml: -------------------------------------------------------------------------------- 1 | 2 | services: 3 | happyr.google.geocoder.geocode: 4 | class: HappyR\Google\GeocoderBundle\Services\GeocodeService 5 | arguments: ["@happyr.google.geocoder.scraper", %happy_r_google_geocoder%] 6 | 7 | happyr.google.geocoder.places.autocomplete: 8 | class: HappyR\Google\GeocoderBundle\Services\PlacesAutocompleteService 9 | arguments: ["@happyr.google.geocoder.scraper", %happy_r_google_geocoder%] 10 | 11 | happyr.google.geocoder.scraper: 12 | class: HappyR\Google\GeocoderBundle\Services\ScraperService 13 | arguments: [] 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | - 5.6 8 | - hhvm 9 | 10 | env: 11 | - SYMFONY_VERSION=2.3.* 12 | 13 | before_script: 14 | - composer self-update 15 | - composer require symfony/symfony:${SYMFONY_VERSION} --prefer-source 16 | 17 | script: phpunit --coverage-text 18 | 19 | matrix: 20 | allow_failures: 21 | - env: SYMFONY_VERSION=dev-master 22 | - php: hhvm 23 | include: 24 | - php: 5.5 25 | env: SYMFONY_VERSION=2.4.* 26 | - php: 5.5 27 | env: SYMFONY_VERSION=2.5.* 28 | - php: 5.5 29 | env: SYMFONY_VERSION=dev-master 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "happyr/google-maps-geocoder-bundle", 3 | "type": "symfony-bundle", 4 | "description": "A Symfony2 bundle to geocode places with google maps", 5 | "keywords": ["Google API"], 6 | "homepage": "http://developer.happyr.com/symfony2-bundles/google-maps-geocoder-bundle", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Tobias Nyholm", 11 | "email": "tobias@happyr.com" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.3.2", 16 | "symfony/yaml": "2.*" 17 | }, 18 | "require-dev": { 19 | "mockery/mockery": "*" 20 | }, 21 | "autoload": { 22 | "psr-0": { "HappyR\\Google\\GeocoderBundle": "" } 23 | }, 24 | "target-dir": "HappyR/Google/GeocoderBundle" 25 | 26 | } 27 | -------------------------------------------------------------------------------- /DependencyInjection/Configuration.php: -------------------------------------------------------------------------------- 1 | root('happy_r_google_geocoder'); 21 | 22 | $rootNode 23 | ->children() 24 | ->scalarNode('developer_key')->defaultValue('')->end() 25 | //list of supported languages https://developers.google.com/maps/faq#languagesupport 26 | ->scalarNode('language')->defaultValue('')->end() 27 | ->end(); 28 | 29 | return $treeBuilder; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Services/ScraperService.php: -------------------------------------------------------------------------------- 1 | processConfiguration($configuration, $configs); 24 | 25 | $container->setParameter('happy_r_google_geocoder', $config); 26 | 27 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); 28 | $loader->load('services.yml'); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Services/PlacesAutocompleteService.php: -------------------------------------------------------------------------------- 1 | scraper = $scraper; 28 | $this->baseUrl .= '&key=' . $config['developer_key']; 29 | 30 | if (isset($config['language']) && $config['language'] != '') { 31 | $this->baseUrl .= '&language=' . $config['language']; 32 | } 33 | } 34 | 35 | /** 36 | * Autocomplete the location 37 | * 38 | * 39 | * @param string $location 40 | * 41 | * @return string the response of a request to google 42 | */ 43 | public function autocomplete($location) 44 | { 45 | $url = $this->baseUrl . '&input=' . urlencode($location); 46 | $response = $this->scraper->scrape($url); 47 | 48 | $response = json_decode($response); 49 | 50 | if ($response->status != 'OK') { 51 | return $location; 52 | } 53 | 54 | return $response->predictions[0]->description; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 15 | 16 | 17 | 18 | 19 | ./Tests 20 | 21 | 22 | 23 | 24 | 26 | 27 | 28 | 29 | 30 | 31 | ./vendor/ 32 | ./Tests/ 33 | ./DataFixtures/ 34 | ./Resources/ 35 | ./DependencyInjection/ 36 | 37 | 38 | ./ 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Services/GeocodeService.php: -------------------------------------------------------------------------------- 1 | scraper = $scraper; 28 | 29 | if (isset($config['language']) && $config['language'] != '') { 30 | $this->baseUrl .= '&language=' . $config['language']; 31 | } 32 | } 33 | 34 | /** 35 | * Geocode an address. 36 | * 37 | * @param string $text 38 | * @param bool $raw 39 | * 40 | * @return string|null the response of a request to google 41 | */ 42 | public function geocodeAddress($text, $raw = false) 43 | { 44 | $response = $this->scraper->scrape($this->baseUrl . '&address=' . urlencode($text)); 45 | 46 | $response = json_decode($response); 47 | 48 | if ($response->status != 'OK') { 49 | $this->handleError($response); 50 | } 51 | 52 | if ($raw) { 53 | return $response->results; 54 | } 55 | 56 | return $response->results[0]->geometry->location; 57 | } 58 | 59 | /** 60 | * Returns address from a coordinates 61 | * 62 | * @param float $lat 63 | * @param float $lang 64 | * @param bool $raw 65 | * 66 | * @return string|null 67 | */ 68 | public function reverseGeocodeAddress($lat, $lang, $raw = false) 69 | { 70 | $response = $this->scraper->scrape($this->baseUrl . '&latlng=' . $lat . ',' . $lang); 71 | 72 | $response = json_decode($response); 73 | 74 | if ($response->status != 'OK') { 75 | $this->handleError($response); 76 | } 77 | 78 | if ($raw) { 79 | return $response->results; 80 | } 81 | 82 | return $response->results[0]->formatted_address; 83 | } 84 | 85 | public function handleError($response) 86 | { 87 | if (isset($response->error_message)) { 88 | $msg = $response->error_message; 89 | } else { 90 | $msg = 'There has been an error while communicating with Google API'; 91 | } 92 | throw new \Exception($msg); 93 | } 94 | } 95 | --------------------------------------------------------------------------------