forked from yii2mod/yii2-google-maps-markers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleMaps.php
263 lines (232 loc) · 6.7 KB
/
GoogleMaps.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
namespace yii2mod\google\maps\markers;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\View;
/**
* GoogleMaps displays a set of user addresses as markers on the map.
*
* To use GoogleMaps, you need to configure its [[userLocations]] property. For example:
*
* ```php
* echo yii2mod\google\maps\markers\GoogleMaps::widget([
* 'userLocations' => [
* [
* 'location' => [
* 'address' => 'Kharkov',
* 'country' => 'Ukraine',
* ],
* 'htmlContent' => '<h1>Kharkov</h1>'
* ],
* [
* 'location' => [
* 'city' => 'New York',
* 'country' => 'Usa',
* ],
* 'htmlContent' => '<h1>New York</h1>'
* ],
* ]
* ]);
* ```
*/
class GoogleMaps extends Widget
{
/**
* @var array user locations array
*/
public $userLocations = [];
/**
* @var string main wrapper height
*/
public $wrapperHeight = '500px';
/**
* @var string google maps url
*/
public $googleMapsUrl = 'https://maps.googleapis.com/maps/api/js?';
/**
* libraries - Example: geometry, places. Default - empty string
* version - 3.exp (Default)
*
* @var array google maps url options(v, language, key, libraries)
*/
public $googleMapsUrlOptions = [];
/**
* Google Maps options (mapTypeId, tilt, zoom, etc...)
*
* @see https://developers.google.com/maps/documentation/javascript/reference
*
* @var array
*/
public $googleMapsOptions = [];
/**
* Example listener for infowindow object:
*
* ```php
* [
* [
* 'object' => 'infowindow',
* 'event' => 'domready',
* 'handler' => (new \yii\web\JsExpression('function() {
* // your custom js code
* }'))
* ]
* ]
* ```
*
* @var array google map listeners
*/
public $googleMapsListeners = [];
/**
* @see https://developers.google.com/maps/documentation/javascript/reference#InfoWindowOptions
*
* @var array
*/
public $infoWindowOptions = [];
/**
* @var string google maps container id
*/
public $containerId = 'map_canvas';
/**
* @var bool render empty map, if userLocations is empty. Defaults to 'true'
*/
public $renderEmptyMap = true;
/**
* Json array for yii.googleMapManager with users address and html contents
*
* @var array
*/
protected $geocodeData = [];
/**
* Init widget
*/
public function init()
{
parent::init();
if (is_array($this->userLocations) === false) {
throw new InvalidConfigException('The "userLocations" property must be of the type array');
}
$this->googleMapsOptions = $this->getGoogleMapsOptions();
$this->infoWindowOptions = $this->getInfoWindowOptions();
$this->googleMapsUrlOptions = $this->getGoogleMapsUrlOptions();
}
/**
* Executes the widget.
*/
public function run()
{
if (empty($this->userLocations) && $this->renderEmptyMap === false) {
return;
}
$this->geocodeData = $this->getGeoCodeData();
echo Html::beginTag('div', ['id' => $this->getId(), 'style' => "height: {$this->wrapperHeight}"]);
echo Html::tag('div', '', ['id' => $this->containerId]);
echo Html::endTag('div');
$this->registerAssets();
parent::run();
}
/**
* Register assets
*/
protected function registerAssets()
{
$view = $this->getView();
GoogleMapsAsset::register($view);
$view->registerJsFile($this->getGoogleMapsApiUrl(), ['position' => View::POS_HEAD]);
$options = $this->getClientOptions();
$view->registerJs("yii.googleMapManager.initModule({$options})", $view::POS_END, 'google-api-js');
}
/**
* Get place urls and htmlContent
*
* @return string
*/
protected function getGeoCodeData()
{
$result = [];
foreach ($this->userLocations as $data) {
$result[] = [
'country' => ArrayHelper::getValue($data['location'], 'country'),
'address' => implode(',', ArrayHelper::getValue($data, 'location')),
'htmlContent' => ArrayHelper::getValue($data, 'htmlContent'),
];
}
return $result;
}
/**
* Get google maps api url
*
* @return string
*/
protected function getGoogleMapsApiUrl()
{
return $this->googleMapsUrl . http_build_query($this->googleMapsUrlOptions);
}
/**
* Get google maps url options
*
* @return array
*/
protected function getGoogleMapsUrlOptions()
{
if (isset(Yii::$app->params['googleMapsUrlOptions']) && empty($this->googleMapsUrlOptions)) {
$this->googleMapsUrlOptions = Yii::$app->params['googleMapsUrlOptions'];
}
return ArrayHelper::merge([
'v' => '3.exp',
'key' => null,
'libraries' => null,
'language' => 'en',
], $this->googleMapsUrlOptions);
}
/**
* Get google maps options
*
* @return array
*/
protected function getGoogleMapsOptions()
{
if (isset(Yii::$app->params['googleMapsOptions']) && empty($this->googleMapsOptions)) {
$this->googleMapsOptions = Yii::$app->params['googleMapsOptions'];
}
return ArrayHelper::merge([
'mapTypeId' => 'roadmap',
'tilt' => 45,
'zoom' => 2,
], $this->googleMapsOptions);
}
/**
* Get info window options
*
* @return array
*/
protected function getInfoWindowOptions()
{
if (isset(Yii::$app->params['infoWindowOptions']) && empty($this->infoWindowOptions)) {
$this->infoWindowOptions = Yii::$app->params['infoWindowOptions'];
}
return ArrayHelper::merge([
'content' => '',
'maxWidth' => 350,
], $this->infoWindowOptions);
}
/**
* Get google map client options
*
* @return string
*/
protected function getClientOptions()
{
return Json::encode([
'geocodeData' => $this->geocodeData,
'mapOptions' => $this->googleMapsOptions,
'listeners' => $this->googleMapsListeners,
'containerId' => $this->containerId,
'renderEmptyMap' => $this->renderEmptyMap,
'infoWindowOptions' => $this->infoWindowOptions,
]);
}
}