Skip to content

Commit 08ed3ec

Browse files
jszobodytoin0u
authored andcommitted
Add support for a Google Maps API key
From https://developers.google.com/maps/documentation/geocoding/#api_key: All Geocoding API applications should use an API key. Including a key in your request: - Allows you to monitor your application's API usage in the APIs Console. - Enables per-key instead of per-IP-address quota limits. - Ensures that Google can contact you about your application if necessary.
1 parent 972eed7 commit 08ed3ec

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Geocoder/Provider/GoogleMapsProvider.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Geocoder\Exception\NoResultException;
1414
use Geocoder\Exception\QuotaExceededException;
1515
use Geocoder\Exception\UnsupportedException;
16+
use Geocoder\Exception\InvalidCredentialsException;
1617
use Geocoder\HttpAdapter\HttpAdapterInterface;
1718

1819
/**
@@ -39,19 +40,26 @@ class GoogleMapsProvider extends AbstractProvider implements LocaleAwareProvider
3940
* @var bool
4041
*/
4142
private $useSsl = false;
43+
44+
/**
45+
* @var string
46+
*/
47+
private $key = null;
4248

4349
/**
4450
* @param HttpAdapterInterface $adapter An HTTP adapter.
4551
* @param string $locale A locale (optional).
4652
* @param string $region Region biasing (optional).
4753
* @param bool $useSsl Whether to use an SSL connection (optional)
54+
* @param string $key Google Geocoding API key (optional)
4855
*/
49-
public function __construct(HttpAdapterInterface $adapter, $locale = null, $region = null, $useSsl = false)
56+
public function __construct(HttpAdapterInterface $adapter, $locale = null, $region = null, $useSsl = false, $key = null)
5057
{
5158
parent::__construct($adapter, $locale);
5259

5360
$this->region = $region;
5461
$this->useSsl = $useSsl;
62+
$this->key = $key;
5563
}
5664

5765
/**
@@ -103,6 +111,10 @@ protected function buildQuery($query)
103111
if (null !== $this->getRegion()) {
104112
$query = sprintf('%s&region=%s', $query, $this->getRegion());
105113
}
114+
115+
if (null !== $this->getKey()) {
116+
$query = sprintf('%s&key=%s', $query, $this->getKey());
117+
}
106118

107119
return $query;
108120
}
@@ -129,6 +141,10 @@ protected function executeQuery($query)
129141
throw new NoResultException(sprintf('Could not execute query %s', $query));
130142
}
131143

144+
if('REQUEST_DENIED' === $json->status && 'The provided API key is invalid.' == $json->error_message) {
145+
throw new InvalidCredentialsException(sprintf('API key is invalid %s', $query));
146+
}
147+
132148
// you are over your quota
133149
if ('OVER_QUERY_LIMIT' === $json->status) {
134150
throw new QuotaExceededException(sprintf('Daily quota exceeded %s', $query));
@@ -242,4 +258,14 @@ protected function getRegion()
242258
{
243259
return $this->region;
244260
}
261+
262+
/**
263+
* Returns the configured key or null.
264+
*
265+
* @return string|null
266+
*/
267+
protected function getKey()
268+
{
269+
return $this->key;
270+
}
245271
}

tests/Geocoder/Tests/Provider/GoogleMapsProviderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,14 @@ public function testGetGeocodedDataWithCityDistrict()
286286
$this->assertInternalType('array', $result);
287287
$this->assertEquals('Kalbach-Riedberg', $result['cityDistrict']);
288288
}
289+
290+
/**
291+
* @expectedException \Geocoder\Exception\InvalidCredentialsException
292+
* @expectedExceptionMessage API key is invalid http://maps.googleapis.com/maps/api/geocode/json?address=10%20avenue%20Gambetta%2C%20Paris%2C%20France&sensor=false
293+
*/
294+
public function testGetGeocodedDataWithInavlidApiKey()
295+
{
296+
$provider = new GoogleMapsProvider($this->getMockAdapterReturns('{"error_message":"The provided API key is invalid.", "status":"REQUEST_DENIED"}'));
297+
$provider->getGeocodedData('10 avenue Gambetta, Paris, France');
298+
}
289299
}

0 commit comments

Comments
 (0)