Skip to content

Commit 7e1abcd

Browse files
committed
Add reverse geocoding functionality
1 parent cb31a21 commit 7e1abcd

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,14 @@ formula.
5656
$distance = $fromCoords->distanceTo($toCoords);
5757
```
5858

59+
### Reverse Geocoding
5960

61+
To lookup an address from a set of GPD coordinate, use the `reverseGeocode` method, as shown below.
62+
63+
```php
64+
$address = $simpleGoogleMaps->reverseGeocode(new LatLong(51.5033635, -0.1276248));
65+
```
66+
67+
This method will return a string containing the address found at the specified coordinates. If no address
68+
could be found, `null` will be returned.
6069

src/Objects/SimpleGoogleMaps.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,48 @@ public function geocode(string $address)
106106
return new LatLong($result->geometry->location->lat, $result->geometry->location->lng);
107107

108108
}
109+
110+
/**
111+
* Look ups an LatLng location, and returns a string containing the address of that location.
112+
*
113+
* @param LatLong $latLong
114+
* @return string
115+
* @throws \GuzzleHttp\Exception\GuzzleException
116+
*/
117+
public function reverseGeocode(LatLong $latLong)
118+
{
119+
$queryUrl = $this->authObject->applyToUrl(
120+
$this->baseUrl . "geocode/json?latlng=" . urlencode($latLong->lat.','.$latLong->long)
121+
);
122+
123+
$cacheKey = sha1(serialize([__FUNCTION__, func_get_args()]));
124+
125+
if (($results = $this->cache->get($cacheKey)) === false) {
126+
$response = (new Client())->request('GET', $queryUrl);
127+
$results = json_decode($response->getBody());
128+
}
129+
130+
if (!$results) {
131+
throw new \Exception('Unable to parse response.');
132+
}
133+
134+
if (!empty($results->error_message)) {
135+
throw new \Exception('Error from Google Maps API: '.$results->error_message);
136+
}
137+
138+
if (!$results->results) {
139+
return null;
140+
}
141+
142+
$result = $results->results[0];
143+
144+
if (!isset($result->formatted_address)) {
145+
return null;
146+
}
147+
148+
$this->cache->set($cacheKey, $results);
149+
150+
return (string) $result->formatted_address;
151+
152+
}
109153
}

src/example.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require_once __DIR__.'/../vendor/autoload.php';
33

44
use LangleyFoxall\SimpleGoogleMaps\Factories\SimpleGoogleMapsFactory;
5+
use LangleyFoxall\SimpleGoogleMaps\Objects\LatLong;
56

67
$address1 = "10 Downing St, Westminster, London SW1A UK";
78
$address2 = "Schott House, Drummond Rd, Stafford ST16 3EL";
@@ -12,13 +13,22 @@
1213
// Enterprise / premium plan authentication:
1314
// $simpleGoogleMaps = SimpleGoogleMapsFactory::getByClientNameAndCryptKey(getenv('CLIENT_NAME'), getenv('CRYPT_KEY'));
1415

16+
echo 'Geocoding:'.PHP_EOL;
17+
1518
$fromCoords = $simpleGoogleMaps->geocode($address1);
1619
$toCoords = $simpleGoogleMaps->geocode($address2);
1720

1821
var_dump($fromCoords, $toCoords);
1922

23+
echo 'Distance calculation:'.PHP_EOL;
24+
2025
$distance = $fromCoords->distanceTo($toCoords);
2126

2227
var_dump($distance);
2328

29+
echo 'Reverse geocoding:'.PHP_EOL;
30+
31+
$reverseGeocodeAddress1 = $simpleGoogleMaps->reverseGeocode(new LatLong(51.5033635, -0.1276248));
32+
$reverseGeocodeAddress2 = $simpleGoogleMaps->reverseGeocode(new LatLong(52.8220531, -2.1127185));
2433

34+
var_dump($reverseGeocodeAddress1, $reverseGeocodeAddress2);

0 commit comments

Comments
 (0)