|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2013-2019 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\plugins\geocoder; |
|
8
|
|
|
|
|
9
|
|
|
use dosamigos\leaflet\Plugin; |
|
10
|
|
|
use yii\base\InvalidConfigException; |
|
11
|
|
|
use yii\helpers\ArrayHelper; |
|
12
|
|
|
use yii\web\JsExpression; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* GeoCoder adds geo search capabilities to your leaflet maps |
|
16
|
|
|
* |
|
17
|
|
|
* @see https://github.com/perliedman/leaflet-control-geocoder |
|
18
|
|
|
* @author Antonio Ramirez <[email protected]> |
|
19
|
|
|
* @link http://www.ramirezcobos.com/ |
|
20
|
|
|
* @link http://www.2amigos.us/ |
|
21
|
|
|
* @package dosamigos\leaflet\plugins\geocoder |
|
22
|
|
|
*/ |
|
23
|
|
|
class GeoCoder extends Plugin |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var BaseService the service to use for geo search |
|
27
|
|
|
*/ |
|
28
|
|
|
private $_service; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @inheritdoc |
|
32
|
|
|
* @throws \yii\base\InvalidConfigException |
|
33
|
|
|
*/ |
|
34
|
9 |
|
public function init() |
|
35
|
|
|
{ |
|
36
|
9 |
|
if ($this->_service === null) { |
|
37
|
|
|
throw new InvalidConfigException('"service" cannot be empty.'); |
|
38
|
|
|
} |
|
39
|
9 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Sets the service to use for geocoding |
|
43
|
|
|
* |
|
44
|
|
|
* @param BaseService $service |
|
45
|
|
|
*/ |
|
46
|
9 |
|
public function setService(BaseService $service) |
|
47
|
|
|
{ |
|
48
|
9 |
|
$this->_service = $service; |
|
49
|
9 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return BaseService |
|
53
|
|
|
*/ |
|
54
|
9 |
|
public function getService() |
|
55
|
|
|
{ |
|
56
|
9 |
|
return $this->_service; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the plugin name |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function getPluginName() |
|
64
|
|
|
{ |
|
65
|
3 |
|
return 'plugin:geocoder'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Registers plugin asset bundle |
|
70
|
|
|
* |
|
71
|
|
|
* @param \yii\web\View $view |
|
72
|
|
|
* |
|
73
|
|
|
* @return $this |
|
74
|
|
|
* @codeCoverageIgnore |
|
75
|
|
|
*/ |
|
76
|
|
|
public function registerAssetBundle($view) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->getService()->registerAssetBundle($view); |
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the javascript ready code for the object to render |
|
84
|
|
|
* @return \yii\web\JsExpression |
|
85
|
|
|
* @throws \yii\base\InvalidConfigException |
|
86
|
|
|
*/ |
|
87
|
9 |
|
public function encode() |
|
88
|
|
|
{ |
|
89
|
|
|
|
|
90
|
9 |
|
$this->clientOptions = ArrayHelper::merge( |
|
91
|
|
|
[ |
|
92
|
|
|
'showMarker' => true |
|
93
|
9 |
|
], |
|
94
|
9 |
|
$this->clientOptions |
|
95
|
9 |
|
); |
|
96
|
|
|
|
|
97
|
9 |
|
$this->clientOptions['geocoder'] = $this->getService()->getJs(); |
|
98
|
|
|
|
|
99
|
9 |
|
$options = $this->getOptions(); |
|
100
|
9 |
|
$name = $this->getName(); |
|
101
|
9 |
|
$map = $this->map; |
|
102
|
|
|
|
|
103
|
9 |
|
$js = "new L.Control.Geocoder($options).addTo($map)"; |
|
104
|
|
|
|
|
105
|
9 |
|
if (!empty($name)) { |
|
106
|
3 |
|
$js = "var $name = $js;"; |
|
107
|
3 |
|
} |
|
108
|
|
|
|
|
109
|
9 |
|
return new JsExpression($js); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|