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
|
|
|
use dosamigos\leaflet\types\Icon; |
10
|
|
|
use yii\base\InvalidConfigException; |
11
|
|
|
use yii\web\JsExpression; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Marker is used to put a marker on the map |
15
|
|
|
* |
16
|
|
|
* @see http://leafletjs.com/reference.html#circle |
17
|
|
|
* @author Antonio Ramirez <[email protected]> |
18
|
|
|
* @link http://www.ramirezcobos.com/ |
19
|
|
|
* @link http://www.2amigos.us/ |
20
|
|
|
* @package dosamigos\leaflet\layers |
21
|
|
|
*/ |
22
|
|
|
/** |
23
|
|
|
* @property string $name |
24
|
|
|
* @property \dosamigos\leaflet\types\LatLng $latLng |
25
|
|
|
* @property string $popupContent |
26
|
|
|
* @property bool $openPopup |
27
|
|
|
*/ |
28
|
|
|
class Marker extends Layer |
29
|
|
|
{ |
30
|
|
|
use LatLngTrait; |
31
|
|
|
use PopupTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Sets the marker's icon |
35
|
|
|
* |
36
|
|
|
* @param Icon $icon |
37
|
|
|
*/ |
38
|
3 |
|
public function setIcon($icon) //Icon - if you force the icon as type, the makimarker won't work...:( |
39
|
|
|
{ |
40
|
3 |
|
$this->clientOptions['icon'] = $icon; |
41
|
3 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return \dosamigos\leaflet\types\Icon |
45
|
|
|
*/ |
46
|
3 |
|
public function getIcon() |
47
|
|
|
{ |
48
|
3 |
|
return isset($this->clientOptions['icon']) ? $this->clientOptions['icon'] : null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initializes the marker. |
53
|
|
|
* @throws \yii\base\InvalidConfigException |
54
|
|
|
*/ |
55
|
75 |
|
public function init() |
56
|
|
|
{ |
57
|
75 |
|
parent::init(); |
58
|
75 |
|
if (empty($this->latLng)) { |
59
|
3 |
|
throw new InvalidConfigException("'latLng' attribute cannot be empty."); |
60
|
|
|
} |
61
|
72 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return \yii\web\JsExpression the marker constructor string |
65
|
|
|
*/ |
66
|
63 |
|
public function encode() |
67
|
3 |
|
{ |
68
|
63 |
|
$latLon = $this->getLatLng()->toArray(true); |
69
|
63 |
|
$options = $this->getOptions(); |
70
|
63 |
|
$name = $this->name; |
71
|
63 |
|
$map = $this->map; |
72
|
63 |
|
$js = $this->bindPopupContent("L.marker($latLon, $options)") . ($map !== null ? ".addTo($map)" : ""); |
73
|
63 |
|
if (!empty($name)) { |
74
|
27 |
|
$js = "var $name = $js;"; |
75
|
24 |
|
} |
76
|
63 |
|
$js .= $this->getEvents() . ($map !== null && empty($name)? ";" : ""); |
77
|
|
|
|
78
|
63 |
|
return new JsExpression($js); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|