File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -56,5 +56,14 @@ formula.
56
56
$distance = $fromCoords->distanceTo($toCoords);
57
57
```
58
58
59
+ ### Reverse Geocoding
59
60
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.
60
69
Original file line number Diff line number Diff line change @@ -106,4 +106,48 @@ public function geocode(string $address)
106
106
return new LatLong ($ result ->geometry ->location ->lat , $ result ->geometry ->location ->lng );
107
107
108
108
}
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
+ }
109
153
}
Original file line number Diff line number Diff line change 2
2
require_once __DIR__ .'/../vendor/autoload.php ' ;
3
3
4
4
use LangleyFoxall \SimpleGoogleMaps \Factories \SimpleGoogleMapsFactory ;
5
+ use LangleyFoxall \SimpleGoogleMaps \Objects \LatLong ;
5
6
6
7
$ address1 = "10 Downing St, Westminster, London SW1A UK " ;
7
8
$ address2 = "Schott House, Drummond Rd, Stafford ST16 3EL " ;
12
13
// Enterprise / premium plan authentication:
13
14
// $simpleGoogleMaps = SimpleGoogleMapsFactory::getByClientNameAndCryptKey(getenv('CLIENT_NAME'), getenv('CRYPT_KEY'));
14
15
16
+ echo 'Geocoding: ' .PHP_EOL ;
17
+
15
18
$ fromCoords = $ simpleGoogleMaps ->geocode ($ address1 );
16
19
$ toCoords = $ simpleGoogleMaps ->geocode ($ address2 );
17
20
18
21
var_dump ($ fromCoords , $ toCoords );
19
22
23
+ echo 'Distance calculation: ' .PHP_EOL ;
24
+
20
25
$ distance = $ fromCoords ->distanceTo ($ toCoords );
21
26
22
27
var_dump ($ distance );
23
28
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 ));
24
33
34
+ var_dump ($ reverseGeocodeAddress1 , $ reverseGeocodeAddress2 );
You can’t perform that action at this time.
0 commit comments