Skip to content

Commit 981ed21

Browse files
committed
Merge pull request #306 from toin0u/hotfix-googlemaps
Support Google Maps API key
2 parents 149c880 + 9ade020 commit 981ed21

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,13 @@ A valid api key is required.
144144
### GoogleMapsProvider ###
145145

146146
The `GoogleMapsProvider` named `google_maps` is able to geocode and reverse geocode **street addresses**.
147+
A locale and a region can be set as well as an optional api key. This provider also supports SSL.
147148

148149

149150
### GoogleMapsBusinessProvider ###
150151

151152
The `GoogleMapsBusinessProvider` named `google_maps_business` is able to geocode and reverse geocode **street addresses**.
152-
A valid `Client ID` is required. The private key is optional.
153+
A valid `Client ID` is required. The private key is optional. This provider also supports SSL.
153154

154155

155156
### BingMapsProvider ###

src/Geocoder/Provider/GoogleMapsProvider.php

+17-1
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
/**
@@ -40,18 +41,25 @@ class GoogleMapsProvider extends AbstractProvider implements LocaleAwareProvider
4041
*/
4142
private $useSsl = false;
4243

44+
/**
45+
* @var string
46+
*/
47+
private $apiKey = null;
48+
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 $apiKey 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, $apiKey = null)
5057
{
5158
parent::__construct($adapter, $locale);
5259

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

5765
/**
@@ -104,6 +112,10 @@ protected function buildQuery($query)
104112
$query = sprintf('%s&region=%s', $query, $this->getRegion());
105113
}
106114

115+
if (null !== $this->apiKey) {
116+
$query = sprintf('%s&key=%s', $query, $this->apiKey);
117+
}
118+
107119
return $query;
108120
}
109121

@@ -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));

tests/Geocoder/Tests/Provider/GoogleMapsProviderTest.php

+10
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)