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\types; |
8
|
|
|
|
9
|
|
|
use dosamigos\leaflet\LeafLet; |
10
|
|
|
use yii\base\InvalidConfigException; |
11
|
|
|
use yii\helpers\Json; |
12
|
|
|
use yii\web\JsExpression; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* LatLng |
16
|
|
|
* Represents a geographical point with a certain latitude and longitude. Please, note that |
17
|
|
|
* all Leaflet methods that accept LatLng objects also accept them in a simple Array form and simple object form |
18
|
|
|
* (unless noted otherwise), so these lines are equivalent: |
19
|
|
|
* |
20
|
|
|
* ``` |
21
|
|
|
* use dosamigos\leafletjs\layers\Marker; |
22
|
|
|
* use dosamigos\leafletjs\types\LatLng; |
23
|
|
|
* |
24
|
|
|
* $marker = new Marker(['latLong'=>[50, 30]]); |
25
|
|
|
* $marker = new Marker(new LatLng(['latLng'=>[50,30]])); |
26
|
|
|
* ``` |
27
|
|
|
* |
28
|
|
|
* @see http://leafletjs.com/reference.html#latlng |
29
|
|
|
* @see http://leafletjs.com/reference.html#bounds |
30
|
|
|
* @author Antonio Ramirez <[email protected]> |
31
|
|
|
* @link http://www.ramirezcobos.com/ |
32
|
|
|
* @link http://www.2amigos.us/ |
33
|
|
|
* @package dosamigos\leaflet\types |
34
|
|
|
*/ |
35
|
|
|
class LatLng extends Type implements ArrayableInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var float the latitude in degrees. |
39
|
|
|
*/ |
40
|
|
|
public $lat; |
41
|
|
|
/** |
42
|
|
|
* @var float the longitude in degrees. |
43
|
|
|
*/ |
44
|
|
|
public $lng; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Initializes the object |
48
|
|
|
* @throws \yii\base\InvalidConfigException |
49
|
|
|
*/ |
50
|
147 |
|
public function init() |
51
|
|
|
{ |
52
|
147 |
|
if ($this->lat === null || $this->lng === null) { |
53
|
3 |
|
throw new InvalidConfigException("'lat' and 'lng' attributes cannot be empty."); |
54
|
|
|
} |
55
|
144 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* LatLng is and object to be used |
59
|
|
|
* @return \yii\web\JsExpression the js initialization code of the object |
60
|
|
|
*/ |
61
|
3 |
|
public function encode() |
62
|
|
|
{ |
63
|
3 |
|
return new JsExpression("L.latLng($this->lat, $this->lng)"); // no semicolon |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the lat and lng as array |
68
|
|
|
* |
69
|
|
|
* @param bool $encode whether to return the array json_encoded or raw |
70
|
|
|
* |
71
|
|
|
* @return array|JsExpression |
72
|
|
|
*/ |
73
|
132 |
|
public function toArray($encode = false) |
74
|
|
|
{ |
75
|
132 |
|
$latLng = [$this->lat, $this->lng]; |
76
|
|
|
|
77
|
|
|
return $encode |
|
|
|
|
78
|
132 |
|
? new JsExpression(Json::encode($latLng, LeafLet::JSON_OPTIONS)) |
79
|
132 |
|
: $latLng; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|