|
1
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.'); |
|
2
|
|
|
/** |
|
3
|
|
|
* Google Maps API integration. |
|
4
|
|
|
* |
|
5
|
|
|
* $Id: Gmap.php 4302 2009-05-01 02:49:41Z kiall $ |
|
6
|
|
|
* |
|
7
|
|
|
* @package Gmaps |
|
8
|
|
|
* @author Kohana Team |
|
9
|
|
|
* @copyright (c) 2007-2008 Kohana Team |
|
10
|
|
|
* @license http://kohanaphp.com/license.html |
|
11
|
|
|
*/ |
|
12
|
|
|
class Gmap_Core |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
// Map settings |
|
16
|
|
|
protected $id; |
|
17
|
|
|
protected $options; |
|
18
|
|
|
protected $center; |
|
19
|
|
|
protected $control; |
|
20
|
|
|
protected $overview_control; |
|
21
|
|
|
protected $type_control = false; |
|
22
|
|
|
|
|
23
|
|
|
// Map types |
|
24
|
|
|
protected $types = array(); |
|
25
|
|
|
protected $default_types = array( |
|
26
|
|
|
'G_NORMAL_MAP','G_SATELLITE_MAP','G_HYBRID_MAP','G_PHYSICAL_MAP' |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
|
|
// Markers icons |
|
30
|
|
|
protected $icons = array(); |
|
31
|
|
|
|
|
32
|
|
|
// Map markers |
|
33
|
|
|
protected $markers = array(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Set the GMap center point. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $id HTML map id attribute |
|
39
|
|
|
* @param array $options array of GMap constructor options |
|
|
|
|
|
|
40
|
|
|
* @return void |
|
|
|
|
|
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($id = 'map', $options = null) |
|
43
|
|
|
{ |
|
44
|
|
|
// Set map ID and options |
|
45
|
|
|
$this->id = $id; |
|
46
|
|
|
$this->options = new Gmap_Options((array) $options); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Return GMap javascript url |
|
51
|
|
|
* |
|
52
|
|
|
* @param string API component |
|
53
|
|
|
* @param array API parameters |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function api_url($component = 'jsapi', $parameters = null, $separator = '&') |
|
57
|
|
|
{ |
|
58
|
|
|
if (empty($parameters['ie'])) { |
|
59
|
|
|
// Set input encoding to UTF-8 |
|
60
|
|
|
$parameters['ie'] = 'utf-8'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (empty($parameters['oe'])) { |
|
64
|
|
|
// Set ouput encoding to input encoding |
|
65
|
|
|
$parameters['oe'] = $parameters['ie']; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (empty($parameters['key'])) { |
|
69
|
|
|
// Set the API key last |
|
70
|
|
|
$parameters['key'] = Kohana::config('gmaps.api_key'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return 'http://'.Kohana::config('gmaps.api_domain').'/'.$component.'?'.http_build_query($parameters, '', $separator); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Retrieves the latitude and longitude of an address. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $address address |
|
80
|
|
|
* @return array longitude, latitude |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function address_to_ll($address) |
|
83
|
|
|
{ |
|
84
|
|
|
$lat = null; |
|
85
|
|
|
$lon = null; |
|
86
|
|
|
|
|
87
|
|
|
if ($xml = Gmap::address_to_xml($address)) { |
|
88
|
|
|
// Get the latitude and longitude from the Google Maps XML |
|
89
|
|
|
// NOTE: the order (lon, lat) is the correct order |
|
90
|
|
|
list($lon, $lat) = explode(',', $xml->Response->Placemark->Point->coordinates); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return array($lat, $lon); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Retrieves the XML geocode address lookup. |
|
98
|
|
|
* ! Results of this method are cached for 1 day. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $address adress |
|
101
|
|
|
* @return object SimpleXML |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function address_to_xml($address) |
|
104
|
|
|
{ |
|
105
|
|
|
static $cache; |
|
106
|
|
|
|
|
107
|
|
|
// Load Cache |
|
108
|
|
|
if ($cache === null) { |
|
109
|
|
|
$cache = Cache::instance(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// Address cache key |
|
113
|
|
|
$key = 'gmap-address-'.sha1($address); |
|
114
|
|
|
|
|
115
|
|
|
if ($xml = $cache->get($key)) { |
|
116
|
|
|
// Return the cached XML |
|
117
|
|
|
return simplexml_load_string($xml); |
|
118
|
|
|
} else { |
|
119
|
|
|
// Setup the retry counter and retry delay |
|
120
|
|
|
$remaining_retries = Kohana::config('gmaps.retries'); |
|
121
|
|
|
$retry_delay = Kohana::config('gmaps.retry_delay'); |
|
122
|
|
|
|
|
123
|
|
|
// Set the XML URL |
|
124
|
|
|
$xml_url = Gmap::api_url('maps/geo', array('output' => 'xml', 'q' => $address), '&'); |
|
125
|
|
|
|
|
126
|
|
|
// Disable error reporting while fetching the feed |
|
127
|
|
|
$ER = error_reporting(~E_NOTICE); |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
// Enter the request/retry loop. |
|
130
|
|
|
while ($remaining_retries) { |
|
131
|
|
|
// Load the XML |
|
132
|
|
|
$xml = simplexml_load_file($xml_url); |
|
133
|
|
|
|
|
134
|
|
|
if (is_object($xml) and ($xml instanceof SimpleXMLElement) and (int) $xml->Response->Status->code === 200) { |
|
135
|
|
|
// Cache the XML |
|
136
|
|
|
$cache->set($key, $xml->asXML(), array('gmaps'), 86400); |
|
137
|
|
|
|
|
138
|
|
|
// Since the geocode was successful, theres no need to try again |
|
139
|
|
|
$remaining_retries = 0; |
|
140
|
|
|
} elseif ((int) $xml->Response->Status->code === 620) { |
|
141
|
|
|
/* Goole is rate limiting us - either we're making too many requests too fast, or |
|
142
|
|
|
* we've exceeded the 15k per 24hour limit. */ |
|
143
|
|
|
|
|
144
|
|
|
// Reduce the number of remaining retries |
|
145
|
|
|
$remaining_retries--; |
|
146
|
|
|
if (! $remaining_retries) { |
|
147
|
|
|
return false; |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
// Sleep for $retry_delay microseconds before trying again. |
|
151
|
|
|
usleep($retry_delay); |
|
152
|
|
|
} else { |
|
153
|
|
|
// Invalid XML response |
|
154
|
|
|
$xml = false; |
|
155
|
|
|
|
|
156
|
|
|
// Dont retry. |
|
157
|
|
|
$remaining_retries = 0; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
// Turn error reporting back on |
|
162
|
|
|
error_reporting($ER); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $xml; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Returns an image map |
|
170
|
|
|
* |
|
171
|
|
|
* @param integer $lat latitude or an array of marker points |
|
172
|
|
|
* @param integer $lon longitude |
|
173
|
|
|
* @param integer $zoom zoom level (1-19) |
|
174
|
|
|
* @param string $type map type (roadmap or mobile) |
|
|
|
|
|
|
175
|
|
|
* @param integer $width map width |
|
176
|
|
|
* @param integer $height map height |
|
177
|
|
|
* @return string |
|
178
|
|
|
*/ |
|
179
|
|
|
public static function static_map($lat = 0, $lon = 0, $zoom = 6, $type = null, $width = 300, $height = 300) |
|
180
|
|
|
{ |
|
181
|
|
|
// Valid map types |
|
182
|
|
|
$types = array('roadmap', 'mobile'); |
|
183
|
|
|
|
|
184
|
|
|
// Maximum width and height are 640px |
|
185
|
|
|
$width = min(640, abs($width)); |
|
186
|
|
|
$height = min(640, abs($height)); |
|
187
|
|
|
|
|
188
|
|
|
$parameters['size'] = $width.'x'.$height; |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
// Minimum zoom = 0, maximum zoom = 19 |
|
191
|
|
|
$parameters['zoom'] = max(0, min(19, abs($zoom))); |
|
192
|
|
|
|
|
193
|
|
|
if (in_array($type, $types)) { |
|
194
|
|
|
// Set map type |
|
195
|
|
|
$parameters['maptype'] = $type; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
if (is_array($lat)) { |
|
199
|
|
|
foreach ($lat as $_lat => $_lon) { |
|
200
|
|
|
$parameters['markers'][] = $_lat.','.$_lon; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$parameters['markers'] = implode('|', $parameters['markers']); |
|
204
|
|
|
} else { |
|
205
|
|
|
$parameters['center'] = $lat.','.$lon; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return Gmap::api_url('staticmap', $parameters); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Set the GMap center point. |
|
213
|
|
|
* |
|
214
|
|
|
* @chainable |
|
215
|
|
|
* @param float $lat latitude |
|
216
|
|
|
* @param float $lon longitude |
|
217
|
|
|
* @param integer $zoom zoom level (1-19) |
|
218
|
|
|
* @param string $type default map type |
|
219
|
|
|
* @return Gmap_Core |
|
220
|
|
|
*/ |
|
221
|
|
|
public function center($lat, $lon, $zoom = 6, $type = 'G_NORMAL_MAP') |
|
222
|
|
|
{ |
|
223
|
|
|
$zoom = max(0, min(19, abs($zoom))); |
|
224
|
|
|
$type = ($type != 'G_NORMAL_MAP' and in_array($type, $this->default_types, true)) ? $type : 'G_NORMAL_MAP'; |
|
225
|
|
|
|
|
226
|
|
|
// Set center location, zoom and default map type |
|
227
|
|
|
$this->center = array($lat, $lon, $zoom, $type); |
|
228
|
|
|
|
|
229
|
|
|
return $this; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Set the GMap controls size. |
|
234
|
|
|
* |
|
235
|
|
|
* @chainable |
|
236
|
|
|
* @param string $size small or large |
|
|
|
|
|
|
237
|
|
|
* @return Gmap_Core |
|
238
|
|
|
*/ |
|
239
|
|
|
public function controls($size = null) |
|
240
|
|
|
{ |
|
241
|
|
|
// Set the control type |
|
242
|
|
|
$this->control = (strtolower($size) == 'small') ? 'Small' : 'Large'; |
|
243
|
|
|
|
|
244
|
|
|
return $this; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Set the GMap overview map. |
|
249
|
|
|
* |
|
250
|
|
|
* @chainable |
|
251
|
|
|
* @param integer $width width |
|
|
|
|
|
|
252
|
|
|
* @param integer $height height |
|
|
|
|
|
|
253
|
|
|
* @return Gmap_Core |
|
254
|
|
|
*/ |
|
255
|
|
|
public function overview($width = '', $height = '') |
|
256
|
|
|
{ |
|
257
|
|
|
$size = (is_int($width) and is_int($height)) ? 'new GSize('.$width.','.$height.')' : ''; |
|
258
|
|
|
$this->overview_control = 'map.addControl(new google.maps.OverviewMapControl('.$size.'));'; |
|
259
|
|
|
|
|
260
|
|
|
return $this; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Set the GMap type controls. |
|
265
|
|
|
* by default renders G_NORMAL_MAP, G_SATELLITE_MAP, and G_HYBRID_MAP |
|
266
|
|
|
* |
|
267
|
|
|
* @chainable |
|
268
|
|
|
* @param string $type map type |
|
|
|
|
|
|
269
|
|
|
* @param string $action add or remove map type |
|
270
|
|
|
* @return Gmap_Core |
|
271
|
|
|
*/ |
|
272
|
|
|
public function types($type = null, $action = 'remove') |
|
273
|
|
|
{ |
|
274
|
|
|
$this->type_control = true; |
|
275
|
|
|
|
|
276
|
|
|
if ($type !== null and in_array($type, $this->default_types, true)) { |
|
277
|
|
|
// Set the map type and action |
|
278
|
|
|
$this->types[$type] = (strtolower($action) == 'remove') ? 'remove' : 'add'; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
return $this; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Create a custom marker icon |
|
286
|
|
|
* |
|
287
|
|
|
* @chainable |
|
288
|
|
|
* @param string $name icon name |
|
289
|
|
|
* @param array $options icon options |
|
290
|
|
|
* @return Gmap_Core |
|
291
|
|
|
*/ |
|
292
|
|
|
public function add_icon($name, array $options) |
|
293
|
|
|
{ |
|
294
|
|
|
// Add a new cusotm icon |
|
295
|
|
|
$this->icons[] = new Gmap_Icon($name, $options); |
|
296
|
|
|
|
|
297
|
|
|
return $this; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Set the GMap marker point. |
|
302
|
|
|
* |
|
303
|
|
|
* @chainable |
|
304
|
|
|
* @param float $lat latitude |
|
305
|
|
|
* @param float $lon longitude |
|
306
|
|
|
* @param string $html HTML for info window |
|
307
|
|
|
* @param array $options marker options |
|
308
|
|
|
* @return Gmap_Core |
|
309
|
|
|
*/ |
|
310
|
|
|
public function add_marker($lat, $lon, $html = '', $options = array()) |
|
311
|
|
|
{ |
|
312
|
|
|
// Add a new marker |
|
313
|
|
|
$this->markers[] = new Gmap_Marker($lat, $lon, $html, $options); |
|
314
|
|
|
|
|
315
|
|
|
return $this; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Render the map into GMap Javascript. |
|
320
|
|
|
* |
|
321
|
|
|
* @param string $template template name |
|
322
|
|
|
* @param array $extra extra fields passed to the template |
|
323
|
|
|
* @return string |
|
324
|
|
|
*/ |
|
325
|
|
|
public function render($template = 'gmaps/javascript', $extra = array()) |
|
326
|
|
|
{ |
|
327
|
|
|
// Latitude, longitude, zoom and default map type |
|
328
|
|
|
list($lat, $lon, $zoom, $default_type) = $this->center; |
|
329
|
|
|
|
|
330
|
|
|
// Map |
|
331
|
|
|
$map = 'var map = new google.maps.Map2(document.getElementById("'.$this->id.'"));'; |
|
332
|
|
|
|
|
333
|
|
|
// Map controls |
|
334
|
|
|
$controls[] = empty($this->control) ? '' : 'map.addControl(new google.maps.'.$this->control.'MapControl());'; |
|
|
|
|
|
|
335
|
|
|
|
|
336
|
|
|
// Map Types |
|
337
|
|
|
if ($this->type_control === true) { |
|
338
|
|
|
if (count($this->types) > 0) { |
|
339
|
|
|
foreach ($this->types as $type => $action) { |
|
340
|
|
|
$controls[] = 'map.'.$action.'MapType('.$type.');'; |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
$controls[] = 'map.addControl(new google.maps.MapTypeControl());'; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
if (! empty($this->overview_control)) { |
|
348
|
|
|
$controls[] = $this->overview_control; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
// Map centering |
|
352
|
|
|
$center = 'map.setCenter(new google.maps.LatLng('.$lat.', '.$lon.'), '.$zoom.', '.$default_type.');'; |
|
353
|
|
|
|
|
354
|
|
|
$data = array_merge($extra, array( |
|
355
|
|
|
'map' => $map, |
|
356
|
|
|
'options' => $this->options, |
|
357
|
|
|
'controls' => implode("\n", $controls), |
|
358
|
|
|
'center' => $center, |
|
359
|
|
|
'icons' => $this->icons, |
|
360
|
|
|
'markers' => $this->markers, |
|
361
|
|
|
)); |
|
362
|
|
|
|
|
363
|
|
|
// Render the Javascript |
|
364
|
|
|
return View::factory($template, $data)->render(); |
|
365
|
|
|
} |
|
366
|
|
|
} // End Gmap |
|
367
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.