|
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
|
|
|
/** |
|
12
|
|
|
* Description of Markers |
|
13
|
|
|
* |
|
14
|
|
|
* @author Petr Olišar <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class Markers extends \Nette\Object |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
const DROP = 'DROP', BOUNCE = 'BOUNCE'; |
|
20
|
|
|
|
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
private $markers = array(); |
|
23
|
|
|
|
|
24
|
|
|
/** @var String */ |
|
25
|
|
|
private $iconDefaultPath; |
|
26
|
|
|
|
|
27
|
|
|
/** @var Boolean */ |
|
28
|
|
|
private $bound = FALSE; |
|
29
|
|
|
|
|
30
|
|
|
/** @var Boolean */ |
|
31
|
|
|
private $markerClusterer = FALSE; |
|
32
|
|
|
|
|
33
|
|
|
private $clusterOptions = array(); |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @internal |
|
38
|
|
|
* @param array $markers |
|
39
|
|
|
* @throws \Nette\InvalidArgumentException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function addMarkers(array $markers) |
|
42
|
|
|
{ |
|
43
|
|
|
if(count($markers)) |
|
44
|
|
|
{ |
|
45
|
|
|
foreach($markers as $marker) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->createMarker($marker); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param array $position |
|
55
|
|
|
* @param boolean $animation |
|
56
|
|
|
* @param String $title |
|
57
|
|
|
* @return Markers |
|
58
|
|
|
*/ |
|
59
|
|
|
public function addMarker(array $position, $animation = false, $title = null) |
|
60
|
|
|
{ |
|
61
|
|
|
if (!is_string($animation) && !is_bool($animation)) |
|
62
|
|
|
{ |
|
63
|
|
|
throw new \InvalidArgumentException("Animation must be string or boolean, $animation (" . |
|
64
|
|
|
gettype($animation) . ") was given"); |
|
65
|
|
|
} |
|
66
|
|
|
if (!is_string($title) && $title != null) |
|
67
|
|
|
{ |
|
68
|
|
|
throw new \InvalidArgumentException("Title must be string or null, $title (".gettype($title).") was given"); |
|
69
|
|
|
} |
|
70
|
|
|
$this->markers[] = array( |
|
71
|
|
|
'position' => $position, |
|
72
|
|
|
'title' => $title, |
|
73
|
|
|
'animation' => $animation, |
|
74
|
|
|
'visible' => true |
|
75
|
|
|
); |
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
public function getMarker() |
|
81
|
|
|
{ |
|
82
|
|
|
return end($this->markers); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getMarkers() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->markers; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
public function deleteMarkers() |
|
95
|
|
|
{ |
|
96
|
|
|
$this->markers = array(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param String $message |
|
102
|
|
|
* @param Boolean $autoOpen |
|
103
|
|
|
* @return Markers |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setMessage($message, $autoOpen = false) |
|
106
|
|
|
{ |
|
107
|
|
|
if (!count($this->markers)) |
|
108
|
|
|
{ |
|
109
|
|
|
throw new \InvalidArgumentException("setMessage must be called after addMarker()"); |
|
110
|
|
|
} |
|
111
|
|
|
end($this->markers); // move the internal pointer to the end of the array |
|
112
|
|
|
$key = key($this->markers); |
|
113
|
|
|
$this->markers[$key]['message'] = $message; |
|
114
|
|
|
$this->markers[$key]['autoOpen'] = $autoOpen; |
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* |
|
121
|
|
|
* @param Boolean $cluster |
|
122
|
|
|
* @return \Oli\GoogleAPI\Markers |
|
123
|
|
|
* @throws \InvalidArgumentException |
|
124
|
|
|
*/ |
|
125
|
|
|
public function isMarkerClusterer($cluster = true) |
|
126
|
|
|
{ |
|
127
|
|
|
if (!is_bool($cluster)) |
|
128
|
|
|
{ |
|
129
|
|
|
throw new \InvalidArgumentException("cluster must be boolean, $cluster (".gettype($cluster).") was given"); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$this->markerClusterer = $cluster; |
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* |
|
139
|
|
|
* @return Boolean |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getMarkerClusterer() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->markerClusterer; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param array $options |
|
149
|
|
|
* @return \Oli\GoogleAPI\Markers |
|
150
|
|
|
*/ |
|
151
|
|
|
public function setClusterOptions($options = array()) |
|
152
|
|
|
{ |
|
153
|
|
|
$this->clusterOptions = $options; |
|
154
|
|
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return array |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getClusterOptions() |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->clusterOptions; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param Boolean $bound Show all of markers |
|
168
|
|
|
* @return \Oli\GoogleAPI\MapAPI |
|
169
|
|
|
*/ |
|
170
|
|
|
public function fitBounds($bound = true) |
|
171
|
|
|
{ |
|
172
|
|
|
if (!is_bool($bound)) |
|
173
|
|
|
{ |
|
174
|
|
|
throw new \InvalidArgumentException("fitBounds must be boolean, $bound (".gettype($bound).") was given"); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$this->bound = $bound; |
|
178
|
|
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* |
|
184
|
|
|
* @return Boolean |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getBound() |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->bound; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* |
|
194
|
|
|
* @param Marker\Icon | String $icon |
|
195
|
|
|
*/ |
|
196
|
|
|
public function setIcon($icon) |
|
197
|
|
|
{ |
|
198
|
|
|
end($this->markers); // move the internal pointer to the end of the array |
|
199
|
|
|
$key = key($this->markers); |
|
200
|
|
|
if($icon instanceof Marker\Icon) |
|
201
|
|
|
{ |
|
202
|
|
|
$icon->setUrl(is_null($this->iconDefaultPath) ? $icon->getUrl() : $this->iconDefaultPath . $icon->getUrl()); |
|
203
|
|
|
$this->markers[$key]['icon'] = $icon->getArray(); |
|
204
|
|
|
|
|
205
|
|
|
} else |
|
206
|
|
|
{ |
|
207
|
|
|
$this->markers[$key]['icon'] = is_null($this->iconDefaultPath) ? $icon : $this->iconDefaultPath . $icon; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* |
|
216
|
|
|
* @param String $defaultPath |
|
217
|
|
|
* @return \Oli\GoogleAPI\Markers |
|
218
|
|
|
*/ |
|
219
|
|
|
public function setDefaultIconPath($defaultPath) |
|
220
|
|
|
{ |
|
221
|
|
|
if(!is_null($defaultPath) && |
|
222
|
|
|
!\Nette\Utils\Strings::endsWith($defaultPath, '/') && |
|
223
|
|
|
!\Nette\Utils\Strings::endsWith($defaultPath, '\\')) |
|
224
|
|
|
{ |
|
225
|
|
|
$defaultPath .= DIRECTORY_SEPARATOR; |
|
226
|
|
|
} |
|
227
|
|
|
$this->iconDefaultPath = $defaultPath; |
|
228
|
|
|
return $this; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
|
|
232
|
|
|
public function getDefaultIconPath() |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->iconDefaultPath; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* |
|
240
|
|
|
* @param String $color Color can be 24-bit color or: green, purple, yellow, blue, gray, orange, red |
|
241
|
|
|
* @return \Oli\GoogleAPI\Markers |
|
242
|
|
|
*/ |
|
243
|
|
|
public function setColor($color) |
|
244
|
|
|
{ |
|
245
|
|
|
$allowed = array('green', 'purple', 'yellow', 'blue', 'orange', 'red'); |
|
246
|
|
|
if (!in_array($color, $allowed)) |
|
247
|
|
|
{ |
|
248
|
|
|
if (!\Nette\Utils\Strings::match($color, '~^0x[a-f0-9]{6}$~i')) |
|
249
|
|
|
{ |
|
250
|
|
|
throw new \Nette\InvalidArgumentException('Color must be 24-bit color or from the allowed list.'); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
end($this->markers); // move the internal pointer to the end of the array |
|
254
|
|
|
$key = key($this->markers); |
|
255
|
|
|
$this->markers[$key]['color'] = $color; |
|
256
|
|
|
return $this; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
private function createMarker(array $marker) |
|
261
|
|
|
{ |
|
262
|
|
|
if(!array_key_exists('coordinates', $marker)) |
|
263
|
|
|
{ |
|
264
|
|
|
throw new \Nette\InvalidArgumentException('Coordinates must be set in every marker'); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
$this->addMarker(array_values($marker['coordinates']), |
|
268
|
|
|
isset($marker['animation']) ? $marker['animation'] : false, |
|
269
|
|
|
isset($marker['title']) ? $marker['title'] : null); |
|
270
|
|
|
|
|
271
|
|
|
if(array_key_exists('message', $marker)) |
|
272
|
|
|
{ |
|
273
|
|
|
if(is_array($marker['message'])) |
|
274
|
|
|
{ |
|
275
|
|
|
$message = array_values($marker['message']); |
|
276
|
|
|
$this->setMessage($message[0], $message[1]); |
|
277
|
|
|
} else |
|
278
|
|
|
{ |
|
279
|
|
|
$this->setMessage($marker['message']); |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
if(array_key_exists('icon', $marker)) |
|
284
|
|
|
{ |
|
285
|
|
|
if(is_array($marker['icon'])) |
|
286
|
|
|
{ |
|
287
|
|
|
$icon = new Marker\Icon($marker['icon']['url']); |
|
288
|
|
|
|
|
289
|
|
|
if(array_key_exists('size', $marker['icon'])) |
|
290
|
|
|
{ |
|
291
|
|
|
$icon->setSize($marker['icon']['size']); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
if(array_key_exists('anchor', $marker['icon'])) |
|
295
|
|
|
{ |
|
296
|
|
|
$icon->setAnchor($marker['icon']['anchor']); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
if(array_key_exists('origin', $marker['icon'])) |
|
300
|
|
|
{ |
|
301
|
|
|
$icon->setOrigin($marker['icon']['origin']); |
|
302
|
|
|
} |
|
303
|
|
|
$this->setIcon($icon); |
|
304
|
|
|
|
|
305
|
|
|
} else |
|
306
|
|
|
{ |
|
307
|
|
|
$this->setIcon($marker['icon']); |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
if(array_key_exists('color', $marker)) |
|
312
|
|
|
{ |
|
313
|
|
|
$this->setColor($marker['color']); |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
|