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\layers; |
9
|
|
|
|
10
|
|
|
use dosamigos\leaflet\LeafLet; |
11
|
|
|
use dosamigos\leaflet\types\LatLng; |
12
|
|
|
use dosamigos\leaflet\types\LatLngBounds; |
13
|
|
|
use yii\base\InvalidParamException; |
14
|
|
|
use yii\helpers\Json; |
15
|
|
|
use yii\web\JsExpression; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* PolyLine is a class for drawing a polygon overlay on the map. |
19
|
|
|
* |
20
|
|
|
* @see http://leafletjs.com/reference.html#polyline |
21
|
|
|
* @package dosamigos\leaflet\layers |
22
|
|
|
*/ |
23
|
|
|
/** |
24
|
|
|
* @property string $name |
25
|
|
|
* @property string $popupContent |
26
|
|
|
* @property bool $openPopup |
27
|
|
|
*/ |
28
|
|
|
class PolyLine extends Layer |
29
|
|
|
{ |
30
|
|
|
use PopupTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var LatLng[] |
34
|
|
|
*/ |
35
|
|
|
private $_latLngs = []; |
36
|
|
|
/** |
37
|
|
|
* @var LatLngBounds |
38
|
|
|
*/ |
39
|
|
|
private $_bounds; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $latLngs |
43
|
|
|
* |
44
|
|
|
* @throws \yii\base\InvalidParamException |
45
|
|
|
*/ |
46
|
27 |
|
public function setLatLngs(array $latLngs) |
47
|
|
|
{ |
48
|
27 |
|
foreach ($latLngs as $latLng) { |
49
|
27 |
|
if (!($latLng instanceof LatLng)) { |
50
|
6 |
|
throw new InvalidParamException("Wrong parameter. All items should be of type LatLng."); |
51
|
|
|
} |
52
|
21 |
|
} |
53
|
21 |
|
$this->_latLngs = $latLngs; |
54
|
21 |
|
$this->setBounds(); |
55
|
21 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return \dosamigos\leaflet\types\LatLng[] |
59
|
|
|
*/ |
60
|
21 |
|
public function getLatLngs() |
61
|
|
|
{ |
62
|
21 |
|
return $this->_latLngs; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the latLngs as array objects |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
21 |
|
public function getLatLngstoArray() |
70
|
|
|
{ |
71
|
21 |
|
$latLngs = []; |
72
|
21 |
|
foreach ($this->getLatLngs() as $latLng) { |
73
|
21 |
|
$latLngs[] = $latLng->toArray(); |
74
|
21 |
|
} |
75
|
21 |
|
return $latLngs; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the LatLngBounds of the polyline. |
80
|
|
|
* @return LatLngBounds |
81
|
|
|
*/ |
82
|
18 |
|
public function getBounds() |
83
|
|
|
{ |
84
|
18 |
|
return $this->_bounds; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Sets bounds after initialization of the [[LatLng]] objects that compound the polyline. |
89
|
|
|
*/ |
90
|
21 |
|
protected function setBounds() |
91
|
|
|
{ |
92
|
21 |
|
$this->_bounds = LatLngBounds::getBoundsOfLatLngs($this->getLatLngs()); |
93
|
21 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the javascript ready code for the object to render |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
9 |
|
public function encode() |
100
|
|
|
{ |
101
|
9 |
|
$latLngs = Json::encode($this->getLatLngstoArray(), LeafLet::JSON_OPTIONS); |
102
|
9 |
|
$options = $this->getOptions(); |
103
|
9 |
|
$name = $this->name; |
104
|
9 |
|
$map = $this->map; |
105
|
9 |
|
$js = $this->bindPopupContent("L.polyline($latLngs, $options)") . ($map !== null ? ".addTo($map);" : ""); |
106
|
9 |
|
if (!empty($name)) { |
107
|
3 |
|
$js = "var $name = $js" . ($map !== null ? "" : ";"); |
108
|
3 |
|
} |
109
|
|
|
|
110
|
9 |
|
return new JsExpression($js); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|