|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2013-2015 2amigOS! Consulting Group LLC |
|
4
|
|
|
* @link http://2amigos.us |
|
5
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace dosamigos\leaflet\layers; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
use yii\base\Component; |
|
11
|
|
|
use yii\base\InvalidParamException; |
|
12
|
|
|
use yii\helpers\ArrayHelper; |
|
13
|
|
|
use yii\helpers\Json; |
|
14
|
|
|
use yii\web\JsExpression; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* LayerGroup Used to group several layers and handle them as one. |
|
18
|
|
|
* |
|
19
|
|
|
* @see http://leafletjs.com/reference.html#layergroup |
|
20
|
|
|
* @author Antonio Ramirez <[email protected]> |
|
21
|
|
|
* @link http://www.ramirezcobos.com/ |
|
22
|
|
|
* @link http://www.2amigos.us/ |
|
23
|
|
|
* @package dosamigos\leaflet\layers |
|
24
|
|
|
*/ |
|
25
|
|
|
class LayerGroup extends Component |
|
26
|
|
|
{ |
|
27
|
|
|
use NameTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string the name of the javascript variable that will hold the reference |
|
31
|
|
|
* to the map object. |
|
32
|
|
|
*/ |
|
33
|
|
|
public $map; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Layer[] |
|
37
|
|
|
*/ |
|
38
|
|
|
private $_layers = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Adds a layer to the group. If no name given it will be automatically generated. |
|
42
|
|
|
* |
|
43
|
|
|
* @param Layer $layer |
|
44
|
|
|
* |
|
45
|
|
|
* @return $this |
|
46
|
|
|
* @throws \yii\base\InvalidParamException |
|
47
|
|
|
*/ |
|
48
|
48 |
|
public function addLayer(Layer $layer) |
|
49
|
|
|
{ |
|
50
|
48 |
|
if (($layer instanceof Popup) || ($layer instanceof TileLayer)) { |
|
51
|
3 |
|
throw new InvalidParamException("'\$layer' cannot be of type Popup or TileLayer."); |
|
52
|
|
|
} |
|
53
|
45 |
|
$layer->map = null; |
|
54
|
45 |
|
$this->_layers[$layer->getName(true)] = $layer; |
|
55
|
45 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns a specific layer. Please note that if the layer didn't have a name, it will be dynamically created. This |
|
60
|
|
|
* method works for those that we know the name previously. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $name the name of the layer |
|
63
|
|
|
* |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
3 |
|
public function getLayer($name) |
|
67
|
|
|
{ |
|
68
|
3 |
|
return ArrayHelper::getValue($this->_layers, $name); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Removes a layer with the given name from the group. |
|
73
|
|
|
* |
|
74
|
|
|
* @param $name |
|
75
|
|
|
* |
|
76
|
|
|
* @return mixed|null |
|
77
|
|
|
*/ |
|
78
|
3 |
|
public function removeLayer($name) |
|
79
|
|
|
{ |
|
80
|
3 |
|
return ArrayHelper::remove($this->_layers, $name); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return Layer[] the added layers |
|
85
|
|
|
*/ |
|
86
|
39 |
|
public function getLayers() |
|
87
|
|
|
{ |
|
88
|
39 |
|
return $this->_layers; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return JsExpression |
|
93
|
|
|
*/ |
|
94
|
9 |
|
public function encode() |
|
95
|
|
|
{ |
|
96
|
9 |
|
$js = []; |
|
97
|
9 |
|
$layers = $this->getLayers(); |
|
98
|
9 |
|
$name = $this->getName(); |
|
99
|
9 |
|
$names = str_replace(array('"', "'"), "", Json::encode(array_keys($layers))); |
|
100
|
9 |
|
$map = $this->map; |
|
101
|
9 |
|
foreach ($layers as $layer) { |
|
102
|
9 |
|
$js[] = $layer->encode(); |
|
103
|
9 |
|
} |
|
104
|
9 |
|
$initJs = "L.layerGroup($names)" . ($map !== null ? ".addTo($map);\n" : ""); |
|
105
|
|
|
|
|
106
|
9 |
|
if (empty($name)) { |
|
107
|
6 |
|
$js[] = $initJs . ($map !== null ? "" : ";"); |
|
108
|
6 |
|
} else { |
|
109
|
3 |
|
$js[] = "var $name = $initJs" . ($map !== null ? "" : ";"); |
|
110
|
|
|
} |
|
111
|
9 |
|
return new JsExpression(implode("\n", $js)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns the initialization |
|
116
|
|
|
* @return JsExpression |
|
117
|
|
|
*/ |
|
118
|
15 |
|
public function oneLineEncode() |
|
119
|
|
|
{ |
|
120
|
15 |
|
$map = $this->map; |
|
121
|
15 |
|
$layers = $this->getLayers(); |
|
122
|
15 |
|
$layersJs = []; |
|
123
|
|
|
/** @var \dosamigos\leaflet\layers\Layer $layer */ |
|
124
|
15 |
|
foreach ($layers as $layer) { |
|
125
|
15 |
|
$layer->name = null; |
|
126
|
15 |
|
$layersJs[] = $layer->encode(); |
|
127
|
15 |
|
} |
|
128
|
15 |
|
$js = "L.layerGroup([" . implode(",", $layersJs) . "])" . ($map !== null ? ".addTo($map);" : ""); |
|
129
|
15 |
|
return new JsExpression($js); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|