1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2015 Petr Olišar (http://olisar.eu) |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view |
6
|
|
|
* the file LICENSE.md that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Oli\GoogleAPI; |
10
|
|
|
|
11
|
|
|
use Nette\Application\UI\Control; |
12
|
|
|
use Nette\Application\Responses\JsonResponse; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Description of GoogleAPI |
17
|
|
|
* |
18
|
|
|
* @author Petr Olišar <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class MapAPI extends Control |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
const ROADMAP = 'ROADMAP', SATELLITE = 'SATELLITE', HYBRID = 'HYBRID', TERRAIN = 'TERRAIN', |
24
|
|
|
BICYCLING = 'BICYCLING', DRIVING = 'DRIVING', TRANSIT = 'TRANSIT', WALKING = 'WALKING'; |
25
|
|
|
|
26
|
|
|
/** @var double|string */ |
27
|
|
|
private $width; |
28
|
|
|
|
29
|
|
|
/** @var double|string */ |
30
|
|
|
private $height; |
31
|
|
|
|
32
|
|
|
/** @var array */ |
33
|
|
|
private $coordinates; |
34
|
|
|
|
35
|
|
|
/** @var Integer */ |
36
|
|
|
private $zoom; |
37
|
|
|
|
38
|
|
|
/** @var String */ |
39
|
|
|
private $type; |
40
|
|
|
|
41
|
|
|
/** @var Boolean */ |
42
|
|
|
private $staticMap = FALSE; |
43
|
|
|
|
44
|
|
|
/** @var Boolean */ |
45
|
|
|
private $clickable = FALSE; |
46
|
|
|
|
47
|
|
|
/** @var String */ |
48
|
|
|
private $key; |
49
|
|
|
|
50
|
|
|
/** @var array */ |
51
|
|
|
private $markers = array(); |
52
|
|
|
|
53
|
|
|
/** @var boolean */ |
54
|
|
|
private $bound; |
55
|
|
|
|
56
|
|
|
/** @var boolean */ |
57
|
|
|
private $markerClusterer; |
58
|
|
|
|
59
|
|
|
/** @var array */ |
60
|
|
|
private $clusterOptions; |
61
|
|
|
|
62
|
|
|
/** @var boolean */ |
63
|
|
|
private $scrollable = FALSE; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
private $waypoints; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
private $direction = ['travelmode' => 'DRIVING']; |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
public function __construct() |
79
|
|
|
{ |
80
|
|
|
parent::__construct(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @internal |
85
|
|
|
* @param array $config |
86
|
|
|
*/ |
87
|
|
|
public function setup(array $config) |
88
|
|
|
{ |
89
|
|
|
$this->width = $config['width']; |
90
|
|
|
$this->height = $config['height']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* |
96
|
|
|
* @param array $coordinates (latitude, longitude) - center of the map |
97
|
|
|
* @return \Oli\GoogleAPI\MapAPI |
98
|
|
|
*/ |
99
|
|
|
public function setCoordinates(array $coordinates) |
100
|
|
|
{ |
101
|
|
|
if(!count($coordinates)) |
102
|
|
|
{ |
103
|
|
|
$this->coordinates = array(NULL, NULL); |
104
|
|
|
} else |
105
|
|
|
{ |
106
|
|
|
$this->coordinates = array_values($coordinates); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param double|string $width |
115
|
|
|
* @param double|string $height |
116
|
|
|
* @return MapAPI |
117
|
|
|
*/ |
118
|
|
|
public function setProportions($width, $height) |
119
|
|
|
{ |
120
|
|
|
$this->width = $width; |
121
|
|
|
$this->height = $height; |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* |
128
|
|
|
* @param string $key |
129
|
|
|
* @return \Oli\GoogleAPI\MapAPI |
130
|
|
|
*/ |
131
|
|
|
public function setKey($key) |
132
|
|
|
{ |
133
|
|
|
$this->key = $key; |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* |
140
|
|
|
* @param int $zoom <0, 19> |
141
|
|
|
* @return \Oli\GoogleAPI\MapAPI |
142
|
|
|
* @throws \InvalidArgumentException |
143
|
|
|
* @throws \LogicException |
144
|
|
|
*/ |
145
|
|
|
public function setZoom($zoom) |
146
|
|
|
{ |
147
|
|
|
if (!is_int($zoom)) |
148
|
|
|
{ |
149
|
|
|
throw new \InvalidArgumentException("type must be integer, $zoom (".gettype($zoom).") was given"); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($zoom < 0 || $zoom > 19) |
153
|
|
|
{ |
154
|
|
|
throw new \LogicException('Zoom must be betwen <0, 19>.'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$this->zoom = (int) $zoom; |
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* |
164
|
|
|
* @param String $type |
165
|
|
|
* @return \Oli\GoogleAPI\MapAPI |
166
|
|
|
*/ |
167
|
|
|
public function setType($type) |
168
|
|
|
{ |
169
|
|
|
if($type !== self::HYBRID && $type !== self::ROADMAP && $type !== self::SATELLITE && |
170
|
|
|
$type !== self::TERRAIN) |
171
|
|
|
{ |
172
|
|
|
throw new \InvalidArgumentException; |
173
|
|
|
} |
174
|
|
|
$this->type = $type; |
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
public function setWaypoint($key, $waypoint) |
180
|
|
|
{ |
181
|
|
|
if($key === 'waypoints') |
182
|
|
|
{ |
183
|
|
|
$this->waypoints['waypoints'][] = $waypoint; |
184
|
|
|
|
185
|
|
|
} else |
186
|
|
|
{ |
187
|
|
|
$this->waypoints[$key] = $waypoint; |
188
|
|
|
} |
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
public function setDirection(array $direction) |
194
|
|
|
{ |
195
|
|
|
$this->direction = $direction; |
196
|
|
|
if(!array_key_exists('travelmode', $this->direction)) |
197
|
|
|
{ |
198
|
|
|
$this->direction['travelmode'] = 'DRIVING'; |
199
|
|
|
} |
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return array Width and Height of the map. |
206
|
|
|
*/ |
207
|
|
|
public function getProportions() |
208
|
|
|
{ |
209
|
|
|
return array('width' => $this->width, 'height' => $this->height); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return array Center of the map |
215
|
|
|
*/ |
216
|
|
|
public function getCoordinates() |
217
|
|
|
{ |
218
|
|
|
return $this->coordinates; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return integer Zoom |
224
|
|
|
*/ |
225
|
|
|
public function getZoom() |
226
|
|
|
{ |
227
|
|
|
return $this->zoom; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return String Which map type will be show |
233
|
|
|
*/ |
234
|
|
|
public function getType() |
235
|
|
|
{ |
236
|
|
|
return $this->type; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
public function getKey() |
241
|
|
|
{ |
242
|
|
|
return $this->key; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* |
248
|
|
|
* @param Boolean $staticMap |
249
|
|
|
* @return \Oli\GoogleAPI\MapAPI |
250
|
|
|
* @throws \InvalidArgumentException |
251
|
|
|
*/ |
252
|
|
|
public function isStaticMap($staticMap = TRUE) |
253
|
|
|
{ |
254
|
|
|
if (!is_bool($staticMap)) |
255
|
|
|
{ |
256
|
|
|
throw new \InvalidArgumentException("staticMap must be boolean, $staticMap (".gettype($staticMap).") was given"); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$this->staticMap = $staticMap; |
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
public function getIsStaticMap() |
265
|
|
|
{ |
266
|
|
|
return $this->staticMap; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* |
272
|
|
|
* @param Boolean $clickable |
273
|
|
|
* @return \Oli\GoogleAPI\MapAPI |
274
|
|
|
* @throws \InvalidArgumentException |
275
|
|
|
*/ |
276
|
|
|
public function isClickable($clickable = TRUE) |
277
|
|
|
{ |
278
|
|
|
if (!$this->staticMap) |
279
|
|
|
{ |
280
|
|
|
throw new \InvalidArgumentException("the 'clickable' option only applies to static maps"); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
if (!is_bool($clickable)) |
284
|
|
|
{ |
285
|
|
|
throw new \InvalidArgumentException("clickable must be boolean, $clickable (".gettype($clickable).") was given"); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$this->clickable = $clickable; |
289
|
|
|
return $this; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
|
293
|
|
|
public function getIsClicable() |
294
|
|
|
{ |
295
|
|
|
return $this->clickable; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
|
299
|
|
|
public function isScrollable($scrollable = TRUE) |
300
|
|
|
{ |
301
|
|
|
if (!is_bool($scrollable)) |
302
|
|
|
{ |
303
|
|
|
throw new \InvalidArgumentException("staticMap must be boolean, $scrollable (".gettype($scrollable).") was given"); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
$this->scrollable = $scrollable; |
307
|
|
|
return $this; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
public function getIsScrollable() |
312
|
|
|
{ |
313
|
|
|
return $this->scrollable; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* |
319
|
|
|
* @param \Oli\GoogleAPI\Markers $markers |
320
|
|
|
* @return \Oli\GoogleAPI\MapAPI |
321
|
|
|
*/ |
322
|
|
|
public function addMarkers(Markers $markers) |
323
|
|
|
{ |
324
|
|
|
$this->markers = $markers->getMarkers(); |
325
|
|
|
$this->bound = $markers->getBound(); |
326
|
|
|
$this->markerClusterer = $markers->getMarkerClusterer(); |
327
|
|
|
$this->clusterOptions = $markers->getClusterOptions(); |
328
|
|
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Alias to handleMarkers() |
334
|
|
|
*/ |
335
|
|
|
public function invalidateMarkers() |
336
|
|
|
{ |
337
|
|
|
$this->handleMarkers(); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @see Nette\Application\Control#render() |
343
|
|
|
*/ |
344
|
|
|
public function render() |
345
|
|
|
{ |
346
|
|
|
if ($this->staticMap) |
347
|
|
|
{ |
348
|
|
|
$this->template->height = $this->height; |
|
|
|
|
349
|
|
|
$this->template->width = $this->width; |
|
|
|
|
350
|
|
|
$this->template->zoom = $this->zoom; |
|
|
|
|
351
|
|
|
$this->template->position = $this->coordinates; |
|
|
|
|
352
|
|
|
$this->template->markers = $this->markers; |
|
|
|
|
353
|
|
|
$this->template->clickable = $this->clickable; |
|
|
|
|
354
|
|
|
$this->template->setFile(dirname(__FILE__) . '/static.latte'); |
|
|
|
|
355
|
|
|
} else |
356
|
|
|
{ |
357
|
|
|
$map = array( |
358
|
|
|
'position' => $this->coordinates, |
359
|
|
|
'height' => $this->height, |
360
|
|
|
'width' => $this->width, |
361
|
|
|
'zoom' => $this->zoom, |
362
|
|
|
'type' => $this->type, |
363
|
|
|
'scrollable' => $this->scrollable, |
364
|
|
|
'key' => $this->key, |
365
|
|
|
'bound' => $this->bound, |
366
|
|
|
'cluster' => $this->markerClusterer, |
367
|
|
|
'clusterOptions' => $this->clusterOptions, |
368
|
|
|
'waypoint' => !is_null($this->waypoints) ? array_merge($this->waypoints, $this->direction) : NULL |
369
|
|
|
); |
370
|
|
|
$this->template->map = \Nette\Utils\Json::encode($map); |
|
|
|
|
371
|
|
|
$this->template->setFile(dirname(__FILE__) . '/template.latte'); |
|
|
|
|
372
|
|
|
} |
373
|
|
|
$this->template->render(); |
|
|
|
|
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Send markers to template as JSON |
379
|
|
|
* @internal |
380
|
|
|
*/ |
381
|
|
|
public function handleMarkers() |
382
|
|
|
{ |
383
|
|
|
$this->getPresenter()->sendResponse(new JsonResponse($this->markers)); |
|
|
|
|
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.