Skip to content

Commit e684f49

Browse files
HurricaneHarrywilldurand
authored andcommitted
Added API key to MapQuestProvider
1 parent 39f5303 commit e684f49

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/Geocoder/Provider/MapQuestProvider.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Geocoder\Provider;
1212

13+
use Geocoder\HttpAdapter\HttpAdapterInterface;
1314
use Geocoder\Exception\NoResultException;
1415
use Geocoder\Exception\UnsupportedException;
1516

@@ -21,13 +22,25 @@ class MapQuestProvider extends AbstractProvider implements ProviderInterface
2122
/**
2223
* @var string
2324
*/
24-
const GEOCODE_ENDPOINT_URL = 'http://open.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=1&thumbMaps=false';
25+
const GEOCODE_ENDPOINT_NOKEY_URL = 'http://open.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=1&thumbMaps=false';
26+
27+
/**
28+
* @var string
29+
*/
30+
const GEOCODE_ENDPOINT_URL = 'http://www.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=1&key=%s';
2531

2632
/**
2733
* @var string
2834
*/
2935
const REVERSE_ENDPOINT_URL = 'http://open.mapquestapi.com/geocoding/v1/reverse?lat=%F&lng=%F';
3036

37+
public function __construct(HttpAdapterInterface $adapter, $locale = null, $apiKey = null)
38+
{
39+
parent::__construct($adapter, $locale);
40+
41+
$this->apiKey = $apiKey;
42+
}
43+
3144
/**
3245
* {@inheritDoc}
3346
*/
@@ -38,7 +51,11 @@ public function getGeocodedData($address)
3851
throw new UnsupportedException('The MapQuestProvider does not support IP addresses.');
3952
}
4053

41-
$query = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address));
54+
if (null === $this->apiKey) {
55+
$query = sprintf(self::GEOCODE_ENDPOINT_NOKEY_URL, urlencode($address));
56+
} else {
57+
$query = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $this->apiKey);
58+
}
4259

4360
return $this->executeQuery($query);
4461
}

tests/Geocoder/Tests/Provider/MapQuestProviderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ public function testGetGeocodedData()
2626
$provider->getGeocodedData('foobar');
2727
}
2828

29+
/**
30+
* @expectedException Geocoder\Exception\NoResultException
31+
* @expectedExceptionMessage Could not find results for given query: http://www.mapquestapi.com/geocoding/v1/address?location=foobar&outFormat=json&maxResults=1&key=my-api-key
32+
*/
33+
public function testGetGeocodedDataWithApiKey()
34+
{
35+
$provider = new MapQuestProvider($this->getMockAdapter(), null, $apiKey = 'my-api-key');
36+
$provider->getGeocodedData('foobar');
37+
}
38+
2939
/**
3040
* @expectedException \Geocoder\Exception\NoResultException
3141
* @expectedExceptionMessage Could not execute query http://open.mapquestapi.com/geocoding/v1/address?location=10+avenue+Gambetta%2C+Paris%2C+France&outFormat=json&maxResults=1&thumbMaps=false

0 commit comments

Comments
 (0)