Skip to content

Commit d987747

Browse files
fluxofttoin0u
authored andcommitted
Update MapQuestProvider.php
MapQuest is not returning any useful information for coordinates in London. However, because it does include a locations element in the returned JSON, a results array is returned from the provider, with null values for all fields except the given coordinates. Because a (bad) result is returned, the next provider in my chain is never queried to get the correct data. Modified the provider to only return results if there are any non-empty values to be written. The test file I was using to verify the behavior is below: <?php require_once dirname(dirname(__FILE__)).'/vendor/autoload.php'; header('Content-type: text/plain'); $geocoder = new \Geocoder\Geocoder(); $buzz = new \Buzz\Browser(new \Buzz\Client\Curl()); $adapter = new \Geocoder\HttpAdapter\BuzzHttpAdapter($buzz); $chain = new \Geocoder\Provider\ChainProvider( array( new \Geocoder\Provider\MapQuestProvider($adapter, 'mapquest_api_key'), new \Geocoder\Provider\GoogleMapsBusinessProvider($adapter, 'google_client_id', 'google_private_key') ) ); $geocoder->registerProvider($chain); $coordinatesArray = array( array('38.8953003', '-77.0328011'), // Washington, DC array('48.868865', '2.339355'), // Paris array('51.507276', '-0.12766') // London ); foreach ($coordinatesArray as $coordinates) { $geocoded = $geocoder->reverse($coordinates[0], $coordinates[1]); echo "reverse geocoding $coordinates[0], $coordinates[1]:\n"; print_r($geocoded); }
1 parent 0271825 commit d987747

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/Geocoder/Provider/MapQuestProvider.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,24 @@ protected function executeQuery($query)
144144
$results = array();
145145

146146
foreach ($locations as $location) {
147-
$results[] = array_merge($this->getDefaults(), array(
148-
'latitude' => $location['latLng']['lat'],
149-
'longitude' => $location['latLng']['lng'],
150-
'streetName' => $location['street'] ?: null,
151-
'city' => $location['adminArea5'] ?: null,
152-
'zipcode' => $location['postalCode'] ?: null,
153-
'county' => $location['adminArea4'] ?: null,
154-
'region' => $location['adminArea3'] ?: null,
155-
'country' => $location['adminArea1'] ?: null,
156-
));
147+
if ($location['street'] || $location['postalCode'] || $location['adminArea5'] || $location['adminArea4'] || $location['adminArea3'] || $location['adminArea1']) {
148+
$results[] = array_merge($this->getDefaults(), array(
149+
'latitude' => $location['latLng']['lat'],
150+
'longitude' => $location['latLng']['lng'],
151+
'streetName' => $location['street'] ?: null,
152+
'city' => $location['adminArea5'] ?: null,
153+
'zipcode' => $location['postalCode'] ?: null,
154+
'county' => $location['adminArea4'] ?: null,
155+
'region' => $location['adminArea3'] ?: null,
156+
'country' => $location['adminArea1'] ?: null,
157+
));
158+
}
157159
}
158160

159-
return $results;
161+
if (empty($results)) {
162+
throw new NoResultException(sprintf('Could not find results for given query: %s', $query));
163+
} else {
164+
return $results;
165+
}
160166
}
161167
}

0 commit comments

Comments
 (0)