forked from heartshare/yii2-leaflet-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeafLet.php
385 lines (352 loc) · 9.32 KB
/
LeafLet.php
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
<?php
/**
* @copyright Copyright (c) 2013-2015 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\leaflet;
use dosamigos\leaflet\controls\Control;
use dosamigos\leaflet\layers\Layer;
use dosamigos\leaflet\layers\LayerGroup;
use dosamigos\leaflet\layers\TileLayer;
use dosamigos\leaflet\layers\Polygon;
use dosamigos\leaflet\types\LatLng;
use dosamigos\leaflet\widgets\Map;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\helpers\ArrayHelper;
/**
* Class LeafLet
* @package dosamigos\leaflet
*/
class LeafLet extends Component
{
/**
* @var integer a counter used to generate [[name]] for layers.
* @internal
*/
public static $counter = 0;
/**
* @var string the prefix to the automatically generated object names.
* @see [[generateName()]]
*/
public static $autoNamePrefix = 'l';
/**
* @var string the name to give to the variable. The name of the map specified on the
* [[TileLayer]] component overrides this one.
*/
public $name = 'map';
/**
* @var int the zoom level of the map
*/
public $zoom = 13;
/**
* @var array the options for the underlying LeafLetJs JS component.
* Please refer to the LeafLetJs api reference for possible
* [options](http://leafletjs.com/reference.html).
*/
public $clientOptions = [];
/**
* @var array the event handlers for the underlying LeafletJs JS plugin.
* Please refer to the LeafLetJs js api object options for possible events.
*/
public $clientEvents = [];
/**
* @var Layer[] holding ui layers (do not confuse with map layers, these are markers, popups, polygons, etc)
*/
private $_layers = [];
/**
* @var LayerGroup[] holding layer groups
*/
private $_layerGroups = [];
/**
* @var LatLng sets the center of the map
*/
private $_center;
/**
* Returns the center of the map.
* @return LatLng center of the map.
*/
public function getCenter()
{
return $this->_center;
}
/**
* Sets the center of the map.
*
* @param LatLng $value center of the map.
*/
public function setCenter(LatLng $value)
{
$this->_center = $value;
}
/**
* @var Control[] holding controls to be added to the map.
*/
private $_controls = [];
/**
* @param \dosamigos\leaflet\controls\Control[] $controls
*
* @throws \yii\base\InvalidParamException
*/
public function setControls($controls)
{
foreach ((array)$controls as $control) {
if (!($control instanceof Control)) {
throw new InvalidParamException("All controls must be of type Control.");
}
}
$this->_controls = $controls;
}
/**
* @return \dosamigos\leaflet\controls\Control[]
*/
public function getControls()
{
return $this->_controls;
}
/**
* @param Control $control
*/
public function addControl(Control $control)
{
$this->_controls[] = $control;
}
/**
* @var \dosamigos\leaflet\layers\TileLayer
*/
private $_tileLayer;
/**
* @param \dosamigos\leaflet\layers\TileLayer $tileLayer
*
* @return static the component itself
*/
public function setTileLayer(TileLayer $tileLayer)
{
if (!empty($tileLayer->map) && strcmp($tileLayer->map, $this->name) !== 0) {
$this->name = $tileLayer->map;
}
if (empty($tileLayer->map)) {
$tileLayer->map = $this->name;
}
$this->_tileLayer = $tileLayer;
return $this;
}
/**
* @return \dosamigos\leaflet\layers\TileLayer
*/
public function getTileLayer()
{
return $this->_tileLayer;
}
/**
* @var array holds the js script lines to be registered.
*/
private $_js = [];
/**
* @param string|array $js custom javascript code to be registered.
* *Warning*: This method overrides any previous settings.
*
* @return static the component itself
*/
public function setJs($js)
{
$this->_js = is_array($js) ? $js : (array)$js;
return $this;
}
/**
* @return array the queued javascript code to be registered.
* *Warning*: This method does not include map initialization.
*/
public function getJs()
{
$js = $this->_js;
foreach ($this->getLayers() as $layer) {
if ($layer instanceof Polygon) {
$layerJs = $layer->encode();
$insertAtTheBottom = $layer->insertAtTheBottom ? 'true' : 'false';
$js[] = "$this->name.addLayer($layerJs, $insertAtTheBottom);";
continue;
}
$layer->map = $this->name;
$js[] = $layer->encode();
}
$groups = $this->getEncodedLayerGroups($this->getLayerGroups());
$controls = $this->getEncodedControls($this->getControls());
$plugins = $this->getEncodedPlugins($this->getPlugins()->getInstalledPlugins());
$js = ArrayHelper::merge($js, $groups);
$js = ArrayHelper::merge($js, $controls);
$js = ArrayHelper::merge($js, $plugins);
return $js;
}
/**
* @var PluginManager
*/
private $_plugins;
/**
* @return PluginManager
*/
public function getPlugins()
{
return $this->_plugins;
}
/**
* Installs a plugin
*
* @param Plugin $plugin
*/
public function installPlugin(Plugin $plugin)
{
$plugin->map = $this->name;
$this->getPlugins()->install($plugin);
}
/**
* Removes an installed plugin
*
* @param $plugin
*
* @return mixed|null
*/
public function removePlugin($plugin)
{
return $this->getPlugins()->remove($plugin);
}
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
if (empty($this->center) || empty($this->zoom)) {
throw new InvalidConfigException("'center' and/or 'zoom' attributes cannot be empty.");
}
$this->_plugins = new PluginManager();
$this->clientOptions['center'] = $this->center->toArray(true);
$this->clientOptions['zoom'] = $this->zoom;
}
/**
* Helper method to render the widget. It is also possible to use the widget directly:
* ```
* echo Map::widget(['leafLet' => $leafLetObject, ...]);
* ```
*
* @param array $config
*
* @return string
*/
public function widget($config = [])
{
ob_start();
ob_implicit_flush(false);
/** @var Widget $widget */
$config['leafLet'] = $this;
$widget = new Map($config);
$out = $widget->run();
return ob_get_clean() . $out;
}
/**
* @param Layer $layer the layer script to add to the js script code. It could be any object extending from [[Layer]]
* component (markers, polylines, popup, etc)
*
* @return static the component itself
*/
public function addLayer(Layer $layer)
{
$this->_layers[] = $layer;
return $this;
}
/**
* @return Layer[] the stored layers
*/
public function getLayers()
{
return $this->_layers;
}
/**
* @param LayerGroup $group sets a layer group
*
* @return static the component itself
*/
public function addLayerGroup(LayerGroup $group)
{
$this->_layerGroups[] = $group;
return $this;
}
/**
* @return layers\LayerGroup[] all stored layer groups
*/
public function getLayerGroups()
{
return $this->_layerGroups;
}
/**
* Clears all stored layer groups
* @return static the component itself
*/
public function clearLayerGroups()
{
$this->_layerGroups = null;
return $this;
}
/**
* @param string $js appends javascript code to be registered.
*
* @return static the component itself
*/
public function appendJs($js)
{
$this->_js[] = $js;
return $this;
}
/**
* @param Control[] $controls
*
* @return array
*/
public function getEncodedControls($controls)
{
return $this->getEncodedObjects($controls);
}
/**
* @param LayerGroup[] $groups
*
* @return array
*/
public function getEncodedLayerGroups($groups)
{
return $this->getEncodedObjects($groups);
}
/**
* @param Plugin[] $plugins
*
* @return array
*/
public function getEncodedPlugins($plugins)
{
return $this->getEncodedObjects($plugins);
}
/**
* @return string
*/
public static function generateName()
{
return self::$autoNamePrefix . self::$counter++;
}
/**
* @param $objects
*
* @return array
*/
protected function getEncodedObjects($objects)
{
$js = [];
foreach ((array)$objects as $object) {
if (property_exists($object, 'map')) {
$object->map = $this->name;
}
$js[] = method_exists($object, 'encode') ? $object->encode() : null;
}
return array_filter($js);
}
}