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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
<?php
/**
*
* EGMapGeocodedAddress
*
* Modified by Antonio Ramirez Cobos
* @link http://www.ramirezcobos.com
*
* A class to geocode addresses
* @author Fabrice Bernhard
*
* @copyright
* info as this library is a modified version of Fabrice Bernhard
*
* Copyright (c) 2008 Fabrice Bernhard
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
class EGMapGeocodedAddress
{
protected $raw_address = null;
protected $lat = null;
protected $lng = null;
protected $accuracy = null;
protected $geocoded_city = null;
protected $geocoded_country_code = null;
protected $geocoded_country = null;
protected $geocoded_address = null;
protected $geocoded_street = null;
protected $geocoded_postal_code = null;
/**
* Constructs a gMapGeocodedAddress object from a given $raw_address String
*
* @param string $raw_address
* @author Fabrice Bernhard
*/
public function __construct($raw_address)
{
$this->raw_address = $raw_address;
}
/**
*
* @return string $raw_address
* @author fabriceb
* @since 2009-06-17
*/
public function getRawAddress()
{
return $this->raw_address;
}
/**
* Geocodes the address using the Google Maps CSV webservice
*
* @param EGMapClient $gmap_client
* @return integer $accuracy
* @author Fabrice Bernhard
* @since 2011-04-21 Matt Cheale Updated to parse API V3 JSON response.
*/
public function geocode($gmap_client)
{
$raw_data = $gmap_client->getGeocodingInfo($this->getRawAddress());
$data = CJSON::decode($raw_data);
if ('OK' != $data['status'])
{
return false;
}
$location = $data['results'][0]['geometry'];
$this->lat = $location['location']['lat'];
$this->lng = $location['location']['lng'];
$this->accuracy = $location['location_type'];
return $this->accuracy;
}
/**
* Reverse geocoding
*
* @return integer
* @author Vincent Guillon <vincentg@theodo.fr>
* @since 2010-03-04
* @since 2011-03-23 Matt Cheale Updated mapping from v2 to v3 of the API result format.
*/
public function reverseGeocode($gmap_client)
{
$raw_data = $gmap_client->getReverseGeocodingInfo($this->getLat(), $this->getLng());
$geocoded_array = CJSON::decode($raw_data, true);
if ($geocoded_array['status'] != 'OK')
{
return false;
}
$result = $geocoded_array['results'][0];
$address_components = $result['address_components'];
$this->raw_address = $result['formatted_address'];
$this->geocoded_address = $result['formatted_address'];
$this->accuracy = $result['types'][0];
$map = array(
'street_address' => 'geocoded_street',
'route' => 'geocoded_street',
'country' => 'geocoded_country',
'locality' => 'geocoded_city',
'postal_code' => 'geocoded_postal_code',
);
foreach ($address_components as $component)
{
foreach ($component['types'] as $type)
{
switch ($type)
{
case 'street_address':
case 'route':
$this->geocoded_street = $component['long_name'];
break;
case 'country':
$this->geocoded_country = $component['long_name'];
$this->geocoded_country_code = $component['short_name'];
break;
case 'locality':
$this->geocoded_city = $component['long_name'];
break;
case 'postal_code':
$this->geocoded_postal_code = $component['long_name'];
break;
default:
// Do nothing
}
}
}
return $this->accuracy;
}
/**
* Geocodes the address using the Google Maps XML webservice, which has more information.
* Unknown values will be set to NULL.
* @todo Change to SimpleXML
* @param EGMapClient $gmap_client
* @return integer $accuracy
* @author Fabrice Bernhard
* @since 2010-12-22 modified by utf8_encode removed Antonio Ramirez
* @since 2011-04-21 Matt Cheale Updated to parse API V3 JSON response.
*/
public function geocodeXml($gmap_client)
{
$raw_data = $gmap_client->getGeocodingInfo($this->getRawAddress(), 'xml');
$xml = simplexml_load_string($raw_data);
if ('OK' != $xml->status)
{
return false;
}
foreach ($xml->result->address_component as $component)
{
$longName = (string) $component->long_name;
$shortName = (string) $component->short_name;
foreach ($component->type as $type)
{
switch ($type)
{
case 'street_address':
case 'route':
$this->geocoded_street = $longName;
break;
case 'country':
$this->geocoded_country = $longName;
$this->geocoded_country_code = $shortName;
break;
case 'locality':
$this->geocoded_city = $shortName;
break;
case 'postal_code':
$this->geocoded_postal_code = $longName;
break;
default:
// Do nothing
}
}
}
$this->lat = (double) $xml->result->geometry->location->lat;
$this->lng = (double) $xml->result->geometry->location->lng;
$this->accuracy = (string) $xml->result->geometry->location_type;
return $this->accuracy;
}
/**
* Returns the latitude
* @return float $latitude
*/
public function getLat()
{
return $this->lat;
}
/**
* Returns the longitude
* @return float $longitude
*/
public function getLng()
{
return $this->lng;
}
/**
* Returns the Geocoding accuracy
* @return integer $accuracy
*/
public function getAccuracy()
{
return $this->accuracy;
}
/**
* Returns the address normalized by the Google Maps web service
* @return string $geocoded_address
*/
public function getGeocodedAddress()
{
return $this->geocoded_address;
}
/**
* Returns the city normalized by the Google Maps web service
* @return string $geocoded_city
*/
public function getGeocodedCity()
{
return $this->geocoded_city;
}
/**
* Returns the country code normalized by the Google Maps web service
* @return string $geocoded_country_code
*/
public function getGeocodedCountryCode()
{
return $this->geocoded_country_code;
}
/**
* Returns the country normalized by the Google Maps web service
* @return string $geocoded_country
*/
public function getGeocodedCountry()
{
return $this->geocoded_country;
}
/**
* Returns the postal code normalized by the Google Maps web service
* @return string $geocoded_postal_code
*/
public function getGeocodedPostalCode()
{
return $this->geocoded_postal_code;
}
/**
* Returns the street name normalized by the Google Maps web service
* @return string $geocoded_country_code
*/
public function getGeocodedStreet()
{
return $this->geocoded_street;
}
/**
* @param string $raw raw address to set
*/
public function setRawAddress($raw)
{
$this->raw_address = $raw;
}
/**
* @param float $lat latitude to set
*/
public function setLat($lat)
{
$this->lat = $lat;
}
/**
* @param float $lat longitude to set
*/
public function setLng($lng)
{
$this->lng = $lng;
}
/**
* @param float $lat accuracy to set
*/
public function setAccuracy($accuracy)
{
$this->accuracy = $accuracy;
}
/**
* @param string $val geocoded city
*/
public function setGeocodedCity($val)
{
$this->geocoded_city = $val;
}
/**
* @param string $val geocoded country code
*/
public function setGeocodedCountryCode($val)
{
$this->geocoded_country_code = $val;
}
/**
* @param string $val geocoded country code
*/
public function setGeocodedCountry($val)
{
$this->geocoded_country = $val;
}
/**
* @param string $val geocoded address
*/
public function setGeocodedAddress($val)
{
$this->geocoded_address = $val;
}
/**
* @param string $val geocoded street
*/
public function setGeocodedStreet($val)
{
$this->geocoded_street = $val;
}
/**
* @param string $val geocoded postal_code
*/
public function setGeocodedPostalCode($val)
{
$this->geocoded_postal_code = $val;
}
}
|