Skip to content

Commit 08099f7

Browse files
authored
docs: update google maps code in readme (#1030)
* docs: update google maps code in readme * fix: throw exception if API key is not provided * fix: tests * docs: update google maps docs
1 parent 374d027 commit 08099f7

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ use Geocoder\Query\GeocodeQuery;
8989
use Geocoder\Query\ReverseQuery;
9090

9191
$httpClient = new \Http\Adapter\Guzzle6\Client();
92-
$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient);
92+
$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient, null, 'your-api-key');
9393
$geocoder = new \Geocoder\StatefulGeocoder($provider, 'en');
9494

9595
$result = $geocoder->geocodeQuery(GeocodeQuery::create('Buckingham Palace, London'));

src/Common/Tests/ProviderAggregatorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
namespace Geocoder\Tests;
1414

1515
use Geocoder\Collection;
16-
use Geocoder\Geocoder;
1716
use Geocoder\Model\Address;
1817
use Geocoder\Model\AddressCollection;
1918
use Geocoder\Query\GeocodeQuery;

src/Provider/GoogleMaps/GoogleMaps.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ final class GoogleMaps extends AbstractHttpProvider implements Provider
5252
private $apiKey;
5353

5454
/**
55-
* @var string
55+
* @var string|null
5656
*/
5757
private $clientId;
5858

@@ -68,7 +68,8 @@ final class GoogleMaps extends AbstractHttpProvider implements Provider
6868

6969
/**
7070
* Google Maps for Business
71-
* https://developers.google.com/maps/documentation/business/.
71+
* https://developers.google.com/maps/documentation/business/
72+
* Maps for Business is no longer accepting new signups
7273
*
7374
* @param HttpClient $client An HTTP adapter
7475
* @param string $clientId Your Client ID
@@ -167,6 +168,10 @@ public function getName(): string
167168
*/
168169
private function buildQuery(string $url, string $locale = null, string $region = null): string
169170
{
171+
if (null === $this->apiKey && null === $this->clientId) {
172+
throw new InvalidCredentials('You must provide an API key. Keyless access was removed in June, 2016');
173+
}
174+
170175
if (null !== $locale) {
171176
$url = sprintf('%s&language=%s', $url, $locale);
172177
}

src/Provider/GoogleMaps/Readme.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,42 @@
1010
This is the Google Maps provider from the PHP Geocoder. This is a **READ ONLY** repository. See the
1111
[main repo](https://github.com/geocoder-php/Geocoder) for information and documentation.
1212

13+
## Usage
14+
15+
```php
16+
$httpClient = new \Http\Adapter\Guzzle6\Client();
17+
18+
// You must provide an API key
19+
$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient, null, 'your-api-key');
20+
21+
$result = $geocoder->geocodeQuery(GeocodeQuery::create('Buckingham Palace, London'));
22+
```
23+
24+
All requests require a valid API key, however google does have a [free tier](https://cloud.google.com/maps-platform/pricing/) available.
25+
Please see [this page for information on getting an API key](https://developers.google.com/maps/documentation/geocoding/get-api-key).
26+
27+
### Google Maps for Business
28+
29+
Previously, google offered a "Business" version of their APIs. The service has been deprecated, however existing clients
30+
can use the static `business` method on the provider to create a client:
31+
32+
```php
33+
34+
$httpClient = new \Http\Adapter\Guzzle6\Client();
35+
36+
// Client ID is required. Private key is optional.
37+
$provider = \Geocoder\Provider\GoogleMaps\GoogleMaps::business($httpClient, 'your-client-id', 'your-private-key');
38+
39+
$result = $geocoder->geocodeQuery(GeocodeQuery::create('Buckingham Palace, London'));
40+
```
41+
1342
### Install
1443

1544
```bash
1645
composer require geocoder-php/google-maps-provider
1746
```
1847

19-
### Note
2048

21-
A valid `Client ID` is required for GoogleMaps for Business. The private key is optional.
2249

2350
### Contribute
2451

src/Provider/GoogleMaps/Tests/GoogleMapsTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function getCacheDir()
4141

4242
public function testGetName()
4343
{
44-
$provider = new GoogleMaps($this->getMockedHttpClient());
44+
$provider = new GoogleMaps($this->getMockedHttpClient(), null, 'mock-api-key');
4545
$this->assertEquals('google_maps', $provider->getName());
4646
}
4747

@@ -50,7 +50,7 @@ public function testGetName()
5050
*/
5151
public function testGeocodeWithLocalhostIPv4()
5252
{
53-
$provider = new GoogleMaps($this->getMockedHttpClient());
53+
$provider = new GoogleMaps($this->getMockedHttpClient(), null, 'mock-api-key');
5454
$provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
5555
}
5656

@@ -60,7 +60,7 @@ public function testGeocodeWithLocalhostIPv4()
6060
*/
6161
public function testGeocodeWithLocalhostIPv6()
6262
{
63-
$provider = new GoogleMaps($this->getMockedHttpClient());
63+
$provider = new GoogleMaps($this->getMockedHttpClient(), null, 'mock-api-key');
6464
$provider->geocodeQuery(GeocodeQuery::create('::1'));
6565
}
6666

@@ -80,7 +80,7 @@ public function testGeocodeWithRealIp()
8080
*/
8181
public function testGeocodeWithQuotaExceeded()
8282
{
83-
$provider = new GoogleMaps($this->getMockedHttpClient('{"status":"OVER_QUERY_LIMIT"}'));
83+
$provider = new GoogleMaps($this->getMockedHttpClient('{"status":"OVER_QUERY_LIMIT"}'), null, 'mock-api-key');
8484
$provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
8585
}
8686

@@ -148,7 +148,7 @@ public function testGeocodeBoundsWithRealAddressForNonRooftopLocation()
148148
*/
149149
public function testReverse()
150150
{
151-
$provider = new GoogleMaps($this->getMockedHttpClient());
151+
$provider = new GoogleMaps($this->getMockedHttpClient(), null, 'mock-api-key');
152152
$provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2));
153153
}
154154

@@ -221,7 +221,7 @@ public function testGeocodeWithCityDistrict()
221221
*/
222222
public function testGeocodeWithInvalidApiKey()
223223
{
224-
$provider = new GoogleMaps($this->getMockedHttpClient('{"error_message":"The provided API key is invalid.", "status":"REQUEST_DENIED"}'));
224+
$provider = new GoogleMaps($this->getMockedHttpClient('{"error_message":"The provided API key is invalid.", "status":"REQUEST_DENIED"}'), null, 'mock-api-key');
225225
$provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
226226
}
227227

@@ -274,7 +274,9 @@ public function testCorrectlySerializesComponents()
274274
function (RequestInterface $request) use (&$uri) {
275275
$uri = (string) $request->getUri();
276276
}
277-
)
277+
),
278+
null,
279+
'test-api-key'
278280
);
279281

280282
$query = GeocodeQuery::create('address')->withData('components', [
@@ -291,7 +293,7 @@ function (RequestInterface $request) use (&$uri) {
291293
$this->assertEquals(
292294
'https://maps.googleapis.com/maps/api/geocode/json'.
293295
'?address=address'.
294-
'&components=country%3ASE%7Cpostal_code%3A22762%7Clocality%3ALund',
296+
'&components=country%3ASE%7Cpostal_code%3A22762%7Clocality%3ALund&key=test-api-key',
295297
$uri
296298
);
297299
}
@@ -305,7 +307,9 @@ public function testCorrectlySetsComponents()
305307
function (RequestInterface $request) use (&$uri) {
306308
$uri = (string) $request->getUri();
307309
}
308-
)
310+
),
311+
null,
312+
'test-api-key'
309313
);
310314

311315
$query = GeocodeQuery::create('address')
@@ -319,7 +323,7 @@ function (RequestInterface $request) use (&$uri) {
319323
$this->assertEquals(
320324
'https://maps.googleapis.com/maps/api/geocode/json'.
321325
'?address=address'.
322-
'&components=country%3ASE%7Cpostal_code%3A22762%7Clocality%3ALund',
326+
'&components=country%3ASE%7Cpostal_code%3A22762%7Clocality%3ALund&key=test-api-key',
323327
$uri
324328
);
325329
}

0 commit comments

Comments
 (0)