|
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\controls; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
use dosamigos\leaflet\layers\LayerGroup; |
|
11
|
|
|
use dosamigos\leaflet\layers\TileLayer; |
|
12
|
|
|
use dosamigos\leaflet\LeafLet; |
|
13
|
|
|
use yii\base\InvalidParamException; |
|
14
|
|
|
use yii\helpers\Json; |
|
15
|
|
|
use yii\web\JsExpression; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Layers The layers control gives users the ability to switch between different base layers and switch overlays on/off. |
|
19
|
|
|
* |
|
20
|
|
|
* @see http://leafletjs.com/reference.html#control-layers |
|
21
|
|
|
* @author Antonio Ramirez <[email protected]> |
|
22
|
|
|
* @link http://www.ramirezcobos.com/ |
|
23
|
|
|
* @link http://www.2amigos.us/ |
|
24
|
|
|
* @package dosamigos\leaflet\controls |
|
25
|
|
|
*/ |
|
26
|
|
|
class Layers extends Control |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var \dosamigos\leaflet\layers\TileLayer[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $_baseLayers = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param mixed $baseLayers |
|
35
|
|
|
* |
|
36
|
|
|
* @throws \yii\base\InvalidParamException |
|
37
|
|
|
*/ |
|
38
|
15 |
|
public function setBaseLayers(array $baseLayers) |
|
39
|
|
|
{ |
|
40
|
15 |
|
foreach ($baseLayers as $key => $layer) { |
|
41
|
15 |
|
if (!($layer instanceof TileLayer)) { |
|
42
|
3 |
|
throw new InvalidParamException("All baselayers should be of type TileLayer "); |
|
43
|
|
|
} |
|
44
|
12 |
|
$this->_baseLayers[$key] = $layer; |
|
45
|
12 |
|
} |
|
46
|
12 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return \dosamigos\leaflet\layers\TileLayer[] |
|
50
|
|
|
*/ |
|
51
|
12 |
|
public function getBaseLayers() |
|
52
|
|
|
{ |
|
53
|
12 |
|
return $this->_baseLayers; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return array of encoded base layers |
|
58
|
|
|
*/ |
|
59
|
12 |
|
public function getEncodedBaseLayers() |
|
60
|
|
|
{ |
|
61
|
12 |
|
$layers = []; |
|
62
|
12 |
|
foreach ($this->getBaseLayers() as $key => $layer) { |
|
63
|
12 |
|
$layer->name = null; |
|
64
|
12 |
|
$layers[$key] = new JsExpression(str_replace(";", "", $layer->encode())); |
|
65
|
12 |
|
} |
|
66
|
12 |
|
return $layers; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var \dosamigos\leaflet\layers\Layer[] |
|
71
|
|
|
*/ |
|
72
|
|
|
private $_overlays = []; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param \dosamigos\leaflet\layers\LayerGroup[] $overlays |
|
76
|
|
|
* |
|
77
|
|
|
* @throws \yii\base\InvalidParamException |
|
78
|
|
|
*/ |
|
79
|
18 |
|
public function setOverlays($overlays) |
|
80
|
|
|
{ |
|
81
|
18 |
|
foreach ($overlays as $key => $overlay) { |
|
82
|
18 |
|
if (!($overlay instanceof LayerGroup)) { |
|
83
|
3 |
|
throw new InvalidParamException("All overlays should be of type LayerGroup"); |
|
84
|
|
|
} |
|
85
|
15 |
|
$this->_overlays[$key] = $overlay; |
|
86
|
15 |
|
} |
|
87
|
15 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return \dosamigos\leaflet\layers\Layer[] |
|
91
|
|
|
*/ |
|
92
|
12 |
|
public function getOverlays() |
|
93
|
|
|
{ |
|
94
|
12 |
|
return $this->_overlays; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return array of encoded overlays |
|
99
|
|
|
*/ |
|
100
|
12 |
|
public function getEncodedOverlays() |
|
101
|
|
|
{ |
|
102
|
12 |
|
$overlays = []; |
|
103
|
|
|
/** |
|
104
|
|
|
* @var \dosamigos\leaflet\layers\LayerGroup $overlay |
|
105
|
|
|
*/ |
|
106
|
12 |
|
foreach ($this->getOverlays() as $key => $overlay) { |
|
107
|
12 |
|
$overlays[$key] = $overlay->oneLineEncode(); |
|
108
|
12 |
|
} |
|
109
|
12 |
|
return $overlays; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns the javascript ready code for the object to render |
|
114
|
|
|
* @return \yii\web\JsExpression |
|
115
|
|
|
*/ |
|
116
|
12 |
|
public function encode() |
|
117
|
|
|
{ |
|
118
|
9 |
|
$this->clientOptions['position'] = $this->position; |
|
119
|
9 |
|
$layers = $this->getEncodedBaseLayers(); |
|
120
|
9 |
|
$overlays = $this->getEncodedOverlays(); |
|
121
|
9 |
|
$options = $this->getOptions(); |
|
122
|
9 |
|
$name = $this->name; |
|
123
|
9 |
|
$map = $this->map; |
|
124
|
|
|
|
|
125
|
12 |
|
$layers = empty($layers) ? '{}' : Json::encode($layers, LeafLet::JSON_OPTIONS); |
|
126
|
9 |
|
$overlays = empty($overlays) ? '{}' : Json::encode($overlays, LeafLet::JSON_OPTIONS); |
|
127
|
|
|
|
|
128
|
9 |
|
$js = "L.control.layers($layers, $overlays, $options)" . ($map !== null ? ".addTo($map);" : ""); |
|
129
|
9 |
|
if (!empty($name)) { |
|
130
|
3 |
|
$js = "var $name = $js" . ($map !== null ? "" : ";"); |
|
131
|
3 |
|
} |
|
132
|
9 |
|
return new JsExpression($js); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|