forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleMapsProvider.php
More file actions
261 lines (216 loc) · 7.48 KB
/
Copy pathGoogleMapsProvider.php
File metadata and controls
261 lines (216 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Provider;
use Geocoder\Exception\NoResultException;
use Geocoder\Exception\QuotaExceededException;
use Geocoder\Exception\UnsupportedException;
use Geocoder\Exception\InvalidCredentialsException;
use Geocoder\HttpAdapter\HttpAdapterInterface;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class GoogleMapsProvider extends AbstractProvider implements LocaleAwareProviderInterface
{
/**
* @var string
*/
const ENDPOINT_URL = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false';
/**
* @var string
*/
const ENDPOINT_URL_SSL = 'https://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false';
/**
* @var string
*/
private $region = null;
/**
* @var bool
*/
private $useSsl = false;
/**
* @var string
*/
private $apiKey = null;
/**
* @param HttpAdapterInterface $adapter An HTTP adapter.
* @param string $locale A locale (optional).
* @param string $region Region biasing (optional).
* @param bool $useSsl Whether to use an SSL connection (optional)
* @param string $apiKey Google Geocoding API key (optional)
*/
public function __construct(HttpAdapterInterface $adapter, $locale = null, $region = null, $useSsl = false, $apiKey = null)
{
parent::__construct($adapter, $locale);
$this->region = $region;
$this->useSsl = $useSsl;
$this->apiKey = $apiKey;
}
/**
* {@inheritDoc}
*/
public function getGeocodedData($address)
{
// Google API returns invalid data if IP address given
// This API doesn't handle IPs
if (filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedException('The GoogleMapsProvider does not support IP addresses.');
}
$query = sprintf(
$this->useSsl ? self::ENDPOINT_URL_SSL : self::ENDPOINT_URL,
rawurlencode($address)
);
return $this->executeQuery($query);
}
/**
* {@inheritDoc}
*/
public function getReversedData(array $coordinates)
{
return $this->getGeocodedData(sprintf('%F,%F', $coordinates[0], $coordinates[1]));
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'google_maps';
}
/**
* @param string $query
*
* @return string Query with extra params
*/
protected function buildQuery($query)
{
if (null !== $this->getLocale()) {
$query = sprintf('%s&language=%s', $query, $this->getLocale());
}
if (null !== $this->getRegion()) {
$query = sprintf('%s®ion=%s', $query, $this->getRegion());
}
if (null !== $this->apiKey) {
$query = sprintf('%s&key=%s', $query, $this->apiKey);
}
return $query;
}
/**
* @param string $query
*
* @return array
*/
protected function executeQuery($query)
{
$query = $this->buildQuery($query);
$content = $this->getAdapter()->getContent($query);
if (null === $content) {
throw new NoResultException(sprintf('Could not execute query %s', $query));
}
$json = json_decode($content);
// API error
if (!isset($json)) {
throw new NoResultException(sprintf('Could not execute query %s', $query));
}
if('REQUEST_DENIED' === $json->status && 'The provided API key is invalid.' === $json->error_message) {
throw new InvalidCredentialsException(sprintf('API key is invalid %s', $query));
}
// you are over your quota
if ('OVER_QUERY_LIMIT' === $json->status) {
throw new QuotaExceededException(sprintf('Daily quota exceeded %s', $query));
}
// no result
if (!isset($json->results) || !count($json->results) || 'OK' !== $json->status) {
throw new NoResultException(sprintf('Could not execute query %s', $query));
}
$results = array();
foreach ($json->results as $result) {
$resultset = $this->getDefaults();
// update address components
foreach ($result->address_components as $component) {
foreach ($component->types as $type) {
$this->updateAddressComponent($resultset, $type, $component);
}
}
// update coordinates
$coordinates = $result->geometry->location;
$resultset['latitude'] = $coordinates->lat;
$resultset['longitude'] = $coordinates->lng;
$resultset['bounds'] = null;
if (isset($result->geometry->bounds)) {
$resultset['bounds'] = array(
'south' => $result->geometry->bounds->southwest->lat,
'west' => $result->geometry->bounds->southwest->lng,
'north' => $result->geometry->bounds->northeast->lat,
'east' => $result->geometry->bounds->northeast->lng
);
} elseif ('ROOFTOP' === $result->geometry->location_type) {
// Fake bounds
$resultset['bounds'] = array(
'south' => $coordinates->lat,
'west' => $coordinates->lng,
'north' => $coordinates->lat,
'east' => $coordinates->lng
);
}
$results[] = array_merge($this->getDefaults(), $resultset);
}
return $results;
}
/**
* Update current resultset with given key/value.
*
* @param array $resultset Resultset to update.
* @param string $type Component type.
* @param object $values The component values;
*
* @return array
*/
protected function updateAddressComponent(&$resultset, $type, $values)
{
switch ($type) {
case 'postal_code':
$resultset['zipcode'] = $values->long_name;
break;
case 'locality':
$resultset['city'] = $values->long_name;
break;
case 'administrative_area_level_2':
$resultset['county'] = $values->long_name;
$resultset['countyCode'] = $values->short_name;
break;
case 'administrative_area_level_1':
$resultset['region'] = $values->long_name;
$resultset['regionCode'] = $values->short_name;
break;
case 'country':
$resultset['country'] = $values->long_name;
$resultset['countryCode'] = $values->short_name;
break;
case 'street_number':
$resultset['streetNumber'] = $values->long_name;
break;
case 'route':
$resultset['streetName'] = $values->long_name;
break;
case 'sublocality':
$resultset['cityDistrict'] = $values->long_name;
break;
default:
}
return $resultset;
}
/**
* Returns the configured region or null.
*
* @return string|null
*/
protected function getRegion()
{
return $this->region;
}
}