|
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
|
|
|
|
|
8
|
|
|
namespace dosamigos\leaflet\widgets; |
|
9
|
|
|
|
|
10
|
|
|
use dosamigos\leaflet\LeafLet; |
|
11
|
|
|
use dosamigos\leaflet\LeafLetAsset; |
|
12
|
|
|
use yii\base\InvalidConfigException; |
|
13
|
|
|
use yii\base\Widget; |
|
14
|
|
|
use yii\helpers\ArrayHelper; |
|
15
|
|
|
use yii\helpers\Html; |
|
16
|
|
|
use yii\helpers\Json; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Widget Map renders the map using the LeafLet component configurations for rendering on the view. |
|
20
|
|
|
* *Important* It is very important to specify the height of the widget, whether with a class name or through an inline |
|
21
|
|
|
* style. Failing to configure the height may have unexpected rendering results. |
|
22
|
|
|
* |
|
23
|
|
|
* @package dosamigos\leaflet\widgets |
|
24
|
|
|
*/ |
|
25
|
|
|
class Map extends Widget |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var \dosamigos\leaflet\LeafLet component holding all configuration |
|
29
|
|
|
*/ |
|
30
|
|
|
public $leafLet; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var string the height of the map. Failing to configure the height of the map, will result in |
|
33
|
|
|
* unexpected results. |
|
34
|
|
|
*/ |
|
35
|
|
|
public $height = '200px'; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array the HTML attributes for the widget container tag. |
|
38
|
|
|
*/ |
|
39
|
|
|
public $options = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Initializes the widget. |
|
43
|
|
|
* This method will register the bootstrap asset bundle. If you override this method, |
|
44
|
|
|
* make sure you call the parent implementation first. |
|
45
|
|
|
*/ |
|
46
|
18 |
|
public function init() |
|
47
|
|
|
{ |
|
48
|
18 |
|
parent::init(); |
|
49
|
18 |
|
if (!isset($this->options['id'])) { |
|
50
|
6 |
|
$this->options['id'] = $this->getId(); |
|
51
|
6 |
|
} |
|
52
|
18 |
|
if (empty($this->leafLet) || !($this->leafLet instanceof LeafLet)) { |
|
53
|
3 |
|
throw new InvalidConfigException( |
|
54
|
|
|
"'leafLet' attribute cannot be empty and should be of type LeafLet component." |
|
55
|
3 |
|
); |
|
56
|
|
|
} |
|
57
|
15 |
|
if (is_numeric($this->height)) { |
|
58
|
3 |
|
$this->height .= 'px'; |
|
59
|
3 |
|
} |
|
60
|
|
|
|
|
61
|
15 |
|
Html::addCssStyle($this->options, ['height' => $this->height], false); |
|
62
|
15 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Renders the map |
|
66
|
|
|
* @return string|void |
|
67
|
|
|
*/ |
|
68
|
15 |
|
public function run() |
|
69
|
|
|
{ |
|
70
|
15 |
|
echo "\n" . Html::tag('div', '', $this->options); |
|
71
|
15 |
|
$this->registerScript(); |
|
72
|
15 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Register the script for the map to be rendered according to the configurations on the LeafLet |
|
76
|
|
|
* component. |
|
77
|
|
|
*/ |
|
78
|
15 |
|
public function registerScript() |
|
79
|
|
|
{ |
|
80
|
15 |
|
$view = $this->getView(); |
|
81
|
|
|
|
|
82
|
15 |
|
LeafLetAsset::register($view); |
|
83
|
15 |
|
$this->leafLet->getPlugins()->registerAssetBundles($view); |
|
84
|
|
|
|
|
85
|
15 |
|
$id = $this->options['id']; |
|
86
|
15 |
|
$name = $this->leafLet->name; |
|
87
|
15 |
|
$js = $this->leafLet->getJs(); |
|
88
|
|
|
|
|
89
|
15 |
|
$clientOptions = $this->leafLet->clientOptions; |
|
90
|
|
|
|
|
91
|
|
|
// for map load event to fire, we have to postpone setting view, until events are bound |
|
92
|
|
|
// see https://github.com/Leaflet/Leaflet/issues/3560 |
|
93
|
15 |
|
$lateInitClientOptions['center'] = Json::encode($clientOptions['center']); |
|
|
|
|
|
|
94
|
15 |
|
$lateInitClientOptions['zoom'] = $clientOptions['zoom']; |
|
95
|
15 |
|
if (isset($clientOptions['bounds'])) { |
|
96
|
15 |
|
$lateInitClientOptions['bounds'] = $clientOptions['bounds']; |
|
97
|
|
|
unset($clientOptions['bounds']); |
|
98
|
15 |
|
} |
|
99
|
15 |
|
unset($clientOptions['center']); |
|
100
|
15 |
|
unset($clientOptions['zoom']); |
|
101
|
15 |
|
|
|
102
|
15 |
|
$options = empty($clientOptions) ? '{}' : Json::encode($clientOptions, LeafLet::JSON_OPTIONS); |
|
103
|
|
|
array_unshift($js, "var $name = L.map('$id', $options);"); |
|
104
|
15 |
|
if ($this->leafLet->getTileLayer() !== null) { |
|
105
|
|
|
$js[] = $this->leafLet->getTileLayer()->encode(); |
|
106
|
15 |
|
} |
|
107
|
15 |
|
|
|
108
|
15 |
|
$clientEvents = $this->leafLet->clientEvents; |
|
109
|
15 |
|
|
|
110
|
15 |
|
if (!empty($clientEvents)) { |
|
111
|
|
|
foreach ($clientEvents as $event => $handler) { |
|
112
|
15 |
|
$js[] = "$name.on('$event', $handler);"; |
|
113
|
|
|
} |
|
114
|
15 |
|
} |
|
115
|
15 |
|
|
|
116
|
|
|
if (isset($lateInitClientOptions['bounds'])) { |
|
117
|
|
|
$js[] = "$name.fitBounds({$lateInitClientOptions['bounds']});"; |
|
118
|
|
|
} else { |
|
119
|
|
|
$js[] = "$name.setView({$lateInitClientOptions['center']}, {$lateInitClientOptions['zoom']});"; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$view->registerJs("function {$name}_init(){\n" . implode("\n", $js) . "}\n{$name}_init();"); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.