Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/Provider/LocationIQ/LocationIQ.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Geocoder\Provider\LocationIQ;

use Geocoder\Collection;
use Geocoder\Exception\InvalidArgument;
use Geocoder\Exception\InvalidServerResponse;
use Geocoder\Exception\InvalidCredentials;
use Geocoder\Location;
Expand All @@ -32,7 +33,20 @@ final class LocationIQ extends AbstractHttpProvider implements Provider
/**
* @var string
*/
const BASE_API_URL = 'https://locationiq.org/v1';
const BASE_API_URL = 'https://{region}.locationiq.com/v1';

/**
* @var string
*/
protected $baseUrl;

/**
* @var array
*/
protected $regions = [
'us1',
'eu1',
];

/**
* @var string
Expand All @@ -43,13 +57,19 @@ final class LocationIQ extends AbstractHttpProvider implements Provider
* @param HttpClient $client an HTTP adapter
* @param string $apiKey an API key
*/
public function __construct(HttpClient $client, string $apiKey)
public function __construct(HttpClient $client, string $apiKey, string $region = null)
{
if (empty($apiKey)) {
throw new InvalidCredentials('No API key provided.');
}

$this->apiKey = $apiKey;
if (null === $region) {
$region = $this->regions[0];
} elseif (true !== in_array($region, $this->regions, true)) {
throw new InvalidArgument(sprintf('`region` must be null or one of `%s`', implode('`, `', $this->regions)));
}
$this->baseUrl = str_replace('{region}', $region, self::BASE_API_URL);

parent::__construct($client);
}
Expand Down Expand Up @@ -177,12 +197,12 @@ private function executeQuery(string $url, string $locale = null): string

private function getGeocodeEndpointUrl(): string
{
return self::BASE_API_URL.'/search.php?q=%s&format=xmlv1.1&addressdetails=1&normalizecity=1&limit=%d&key='.$this->apiKey;
return $this->baseUrl.'/search.php?q=%s&format=xmlv1.1&addressdetails=1&normalizecity=1&limit=%d&key='.$this->apiKey;
}

private function getReverseEndpointUrl(): string
{
return self::BASE_API_URL.'/reverse.php?format=xmlv1.1&lat=%F&lon=%F&addressdetails=1&normalizecity=1&zoom=%d&key='.$this->apiKey;
return $this->baseUrl.'/reverse.php?format=xmlv1.1&lat=%F&lon=%F&addressdetails=1&normalizecity=1&zoom=%d&key='.$this->apiKey;
}

private function getNodeValue(\DOMNodeList $element)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
s:2304:"<?xml version="1.0" encoding="UTF-8" ?>
<searchresults timestamp='Wed, 27 Apr 22 14:49:00 +0530' attribution='https://locationiq.com/attribution' querystring='10 Downing St, London, UK' polygon='false'>
<place place_id='274489782' osm_type='relation' osm_id='1879842' boundingbox="51.5032573,51.5036483,-0.1278356,-0.1273038" lat='51.50344025' lon='-0.12770820958562096' display_name='Prime Minister’s Office, 10, Downing Street, Westminster, Covent Garden, Westminster, Greater London, England, SW1A 2AA, United Kingdom' class='tourism' type='attraction' importance='0.91870641220514' icon='https://locationiq.org/static/images/mapicons/poi_point_of_interest.p.20.png'>
<attraction>Prime Minister’s Office</attraction><house_number>10</house_number><road>Downing Street</road><quarter>Westminster</quarter><suburb>Covent Garden</suburb><city>Westminster</city><state_district>Greater London</state_district><state>England</state><postcode>SW1A 2AA</postcode><country>United Kingdom</country><country_code>gb</country_code></place><place place_id='17135717' osm_type='node' osm_id='1931404517' boundingbox="51.5032302,51.5033302,-0.1276858,-0.1275858" lat='51.5032802' lon='-0.1276358' display_name='10, Downing Street, Westminster, Covent Garden, Westminster, Greater London, England, SW1A 2AB, United Kingdom' class='place' type='house' importance='0.421'>
<house_number>10</house_number><road>Downing Street</road><quarter>Westminster</quarter><suburb>Covent Garden</suburb><city>Westminster</city><state_district>Greater London</state_district><state>England</state><postcode>SW1A 2AB</postcode><country>United Kingdom</country><country_code>gb</country_code></place><place place_id='337785034' osm_type='node' osm_id='9529452234' boundingbox="51.5032607,51.5033607,-0.127437,-0.127337" lat='51.5033107' lon='-0.127387' display_name='Downing Street, Westminster, Covent Garden, Westminster, Greater London, England, SW1A 2AA, United Kingdom' class='man_made' type='surveillance' importance='0.311'>
<road>Downing Street</road><quarter>Westminster</quarter><suburb>Covent Garden</suburb><city>Westminster</city><state_district>Greater London</state_district><state>England</state><postcode>SW1A 2AA</postcode><country>United Kingdom</country><country_code>gb</country_code></place></searchresults>";
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
s:402:"<?xml version='1.0' encoding='UTF-8' ?>
<searchresults timestamp='Wed, 08 Jan 20 20:55:53 +0000' attribution='© LocationIQ.com CC BY 4.0, Data © OpenStreetMap contributors, ODbL 1.0' querystring='q=jsajhgsdkfjhsfkjhaldkadjaslgldasd&amp;format=xmlv1.1&amp;addressdetails=1&amp;normalizecity=1&amp;limit=5&amp;key=d4146b4eba1a69&amp;accept-language=en'>
<searchresults timestamp='Wed, 27 Apr 22 09:19:00 +0000' attribution='© LocationIQ.com CC BY 4.0, Data © OpenStreetMap contributors, ODbL 1.0' querystring='q=jsajhgsdkfjhsfkjhaldkadjaslgldasd&amp;format=xmlv1.1&amp;addressdetails=1&amp;normalizecity=1&amp;limit=5&amp;key=d4146b4eba1a69&amp;accept-language=en'>
<error>Unable to geocode</error></searchresults>";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
s:879:"<?xml version="1.0" encoding="UTF-8" ?>
<reversegeocode timestamp='Wed, 27 Apr 22 14:49:01 +0530' attribution='https://locationiq.com/attribution' querystring='format=xmlv1.1&amp;lat=38.900206&amp;lon=-77.036991&amp;addressdetails=1&amp;normalizecity=1&amp;zoom=18&amp;key=d4146b4eba1a69&amp;accept-language=en'>
<result place_id="334190832434" lat="38.900514" lon="-77.036971" boundingbox="38.900514,38.900514,-77.036971,-77.036971">800, 16th Street NW, Downtown, Washington, District of Columbia, District of Columbia, 20006, USA</result><addressparts><house_number>800</house_number><road>16th Street NW</road><neighbourhood>Downtown</neighbourhood><city>Washington</city><county>District of Columbia</county><state>District of Columbia</state><postcode>20006</postcode><country>United States of America</country><country_code>us</country_code></addressparts></reversegeocode>";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
s:1004:"<?xml version="1.0" encoding="UTF-8" ?>
<reversegeocode timestamp='Wed, 27 Apr 22 14:47:55 +0530' attribution='https://locationiq.com/attribution' querystring='format=xmlv1.1&amp;lat=48.860000&amp;lon=2.350000&amp;addressdetails=1&amp;normalizecity=1&amp;zoom=18&amp;key=d4146b4eba1a69'>
<result place_id="333031508" osm_type="node" osm_id="9383736463" lat="48.8600564" lon="2.3499514" boundingbox="48.8600064,48.8601064,2.3499014,2.3500014">Bistrot Beaubourg, 25, Rue Quincampoix, Quartier Saint-Merri, 4th Arrondissement, Paris, Ile-de-France, Metropolitan France, 75004, France</result><addressparts><cafe>Bistrot Beaubourg</cafe><house_number>25</house_number><road>Rue Quincampoix</road><city_block>Quartier Saint-Merri</city_block><suburb>4th Arrondissement</suburb><city_district>Paris</city_district><city>Paris</city><state>Ile-de-France</state><region>Metropolitan France</region><postcode>75004</postcode><country>France</country><country_code>fr</country_code></addressparts></reversegeocode>";
11 changes: 11 additions & 0 deletions src/Provider/LocationIQ/Tests/LocationIQTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public function testGeocodeWithAddressGetsEmptyXML()
$provider->geocodeQuery(GeocodeQuery::create('Läntinen Pitkäkatu 35, Turku'));
}

public function testGeocodeWithInvalidRegion()
{
$this->expectException(\Geocoder\Exception\InvalidArgument::class);

$emptyXML = <<<'XML'
<?xml version="1.0" encoding="utf-8"?><searchresults_empty></searchresults_empty>
XML;

$provider = new LocationIQ($this->getMockedHttpClient($emptyXML), $_SERVER['LOCATIONIQ_API_KEY'], 'invalid');
}

public function testReverseWithCoordinatesGetsError()
{
$errorXml = <<<'XML'
Expand Down