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
|
|
|
|
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\web\JsExpression; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* TileLayer is used to load and display tile layers on the map |
16
|
|
|
* |
17
|
|
|
* @see http://leafletjs.com/reference.html#tilelayer |
18
|
|
|
* @author Antonio Ramirez <[email protected]> |
19
|
|
|
* @link http://www.ramirezcobos.com/ |
20
|
|
|
* @link http://www.2amigos.us/ |
21
|
|
|
* @package dosamigos\leaflet\layers |
22
|
|
|
*/ |
23
|
|
|
class TileLayer extends Layer |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string a template has the following form: |
28
|
|
|
* |
29
|
|
|
* ``` |
30
|
|
|
* 'http://{s}.somedomain.com/blabla/{z}/{x}/{y}.png' |
31
|
|
|
* ``` |
32
|
|
|
* |
33
|
|
|
* {s} means one of the available subdomains (used sequentially to help with browser parallel requests per domain |
34
|
|
|
* limitation; subdomain values are specified in options; a, b or c by default, can be omitted), {z} — zoom level, |
35
|
|
|
* {x} and {y} — tile coordinates. |
36
|
|
|
* |
37
|
|
|
* You can use custom keys in the template, which will be evaluated from TileLayer options, like this: |
38
|
|
|
* |
39
|
|
|
* ``` |
40
|
|
|
* $layer = new TileLayer([ |
41
|
|
|
* 'urlTemplate' => 'L.tileLayer('http://{s}.somedomain.com/{foo}/{z}/{x}/{y}.png', |
42
|
|
|
* 'clientOptions' => [ |
43
|
|
|
* 'foo' => 'bar' |
44
|
|
|
* ] |
45
|
|
|
* ]); |
46
|
|
|
* ``` |
47
|
|
|
*/ |
48
|
|
|
public $urlTemplate; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @throws \yii\base\InvalidConfigException |
52
|
|
|
*/ |
53
|
42 |
|
public function init() |
54
|
|
|
{ |
55
|
42 |
|
parent::init(); |
56
|
42 |
|
if (empty($this->urlTemplate)) { |
57
|
3 |
|
throw new InvalidConfigException("'urlTemplate' cannot be empty."); |
58
|
|
|
} |
59
|
39 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return \yii\web\JsExpression the marker constructor string |
63
|
|
|
*/ |
64
|
36 |
|
public function encode() |
65
|
|
|
{ |
66
|
36 |
|
$options = $this->getOptions(); |
67
|
36 |
|
$name = $this->getName(); |
68
|
36 |
|
$map = $this->map; |
69
|
36 |
|
$js = "L.tileLayer('$this->urlTemplate', $options)" . ($map !== null ? ".addTo($map);" : ""); |
70
|
36 |
|
if (!empty($name)) { |
71
|
3 |
|
$js = "var $name = $js" . ($map !== null ? "" : ";"); |
72
|
3 |
|
$js .= $this->getEvents(); |
73
|
3 |
|
} |
74
|
|
|
|
75
|
36 |
|
return new JsExpression($js); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|