-
Notifications
You must be signed in to change notification settings - Fork 6
/
EGmap3Widget.php
407 lines (374 loc) · 10.3 KB
/
EGmap3Widget.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
/**
* EGmap3 Yii extension
*
* Object oriented PHP interface to GMAP3 Javascript library for
* Google Maps.
*
* GMAP3 was originally written by DEMONTE Jean-Baptiste.
* Special permission has been granted to release under the LGPL for
* this extension.
* @link http://gmap3.net
*
* This extension has taken some ideas (but no code) from Antonio Ramirez'
* egmap Yii extension.
* @link http://www.yiiframework.com/extension/egmap
*
* @copyright © Digitick <www.digitick.net> 2011
* @license GNU Lesser General Public License v3.0
* @author Ianaré Sévi
*
*/
// load these like this so small classes can be combined into fewer files.
require_once 'EGmap3Constants.php';
require_once 'EGmap3MapSubOptions.php';
require_once 'EGmap3Primitives.php';
/**
* Main class.
*
* @author Ianaré Sévi
*/
class EGmap3Widget extends CWidget
{
/**
* @var boolean Use minified version of gmap3 library, defaults to true.
*/
public $mini = true;
/**
* @var integer Width of the map widget. By default uses 'px' but other
* units may be used.
*/
public $width = 550;
/**
* @var integer Height of the map widget. By default uses 'px' but other
* units may be used.
*/
public $height = 400;
/**
* @var string CSS size unit to use, such as : 'px', 'em', 'pt', etc ..
*/
public $unit = 'px';
/**
* @var boolean Set the map widget to be resizable, default is false.
*/
public $resizable = false;
/**
* @var boolean Show a layer that displays bike lanes and paths and demotes
* large roads.
*/
public $bicyclingLayer;
/**
* @var boolean Show a layer that displays current road traffic.
*/
public $trafficLayer;
/**
* @var boolean Zoom the map to automatically fit the elements it contains.
*/
public $autofit;
//
protected $rectangles = array();
protected $polygons = array();
protected $polylines = array();
protected $routes = array();
protected $markers = array();
protected $circles = array();
protected $kmlLayers = array();
protected $styledMaps = array();
/**
* @var EGmap3Map The map itself.
*/
protected $map;
/**
* Create a new Google map widget.
* @param mixed $options Associative array or Options object
*/
public function __construct($options=null)
{
if ($options) {
$this->setOptions($options);
}
}
public function initOptions()
{
if (!is_object($this->map)) {
$this->map = new EGmap3Map();
}
}
/**
* Set the map display size.
* @param integer $width The width.
* @param integer $height The height.
* @param string $unit CSS size unit to use, such as : 'px', 'em', 'pt', etc ..
*/
public function setSize($width, $height, $unit='px')
{
$this->width = $width;
$this->height = $height;
$this->unit = $unit;
}
/**
* Set map options.
* @param EMapOptions $options
*/
public function setOptions($options)
{
$this->initOptions();
$this->map->setOptions($options);
}
/**
* @return EGmap3MapOptions
*/
public function getOptions()
{
$this->initOptions();
return $this->map->getOptions();
}
/**
* Add an action to the map.
*
* @param EGmap3ActionBase $action
*/
public function add(EGmap3ActionBase $action)
{
switch (get_class($action)) {
case 'EGmap3Marker':
$this->markers[] = $action;
break;
case 'EGmap3InfoWindow':
$this->markers[] = $action;
break;
case 'EGmap3Circle':
$this->circles[] = $action;
break;
case 'EGmap3Rectangle':
$this->rectangles[] = $action;
break;
case 'EGmap3Polygon':
$this->polygons[] = $action;
break;
case 'EGmap3Polyline':
$this->polylines[] = $action;
break;
case 'EGmap3Route':
$this->routes[] = $action;
break;
case 'EGmap3StyledMap':
$this->styledMaps[] = $action;
break;
default:
throw new CException('Invalid type given to add.');
break;
}
}
public function getMarkers()
{
return $this->markers;
}
public function getCircles()
{
return $this->markers;
}
public function getRectangles()
{
return $this->rectangles;
}
public function getPolygons()
{
return $this->polygons;
}
public function getRoutes()
{
return $this->routes;
}
public function getStyledMaps()
{
return $this->styledMaps;
}
/**
* Registers the core script files.
* This method registers jquery and JUI JavaScript files and the theme CSS file.
*/
protected function registerCoreScripts()
{
$params = array(
'sensor' => 'false',
'language' => Yii::app()->getLanguage()
);
$http = (Yii::app()->getRequest()->isSecureConnection)?'https':'http';
$apiScript = $http.'://maps.google.com/maps/api/js?' . http_build_query($params);
$assets = Yii::app()->assetManager->publish(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets');
$cs = Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');
$cs->registerScriptFile($apiScript)
->registerScriptFile($assets . (($this->mini) ? '/gmap3.min.js' : '/gmap3.js'));
if ($this->resizable) {
$cs->registerCoreScript('jquery.ui');
$cs->registerCssFile($cs->getCoreScriptUrl().'/jui/css/base/jquery-ui.css');
}
}
/**
* Update a marker on the map using a Yii model's fields. The model fields
* must exist on the page.
*
* @param CModel $model Model to use
* @param array $attributes Model attributes to use for building the address.
* Must be given in the correct order !
* @param array $options Options to set :<ul>
* <li>'first' - set to first marker added to map, default is last
* <li>'nopan' - do not pan the map on marker update.
* <li>'event' => 'change' - valid jQuery event to trigger map update.
* </ul>
*/
public function updateMarkerAddressFromModel(CModel $model, array $attributes, array $options=array())
{
// get attribute IDs
for ($i = 0; $i < count($attributes); $i++) {
$attributes[$i] = CHtml::activeId($model, $attributes[$i]);
}
// build the hidden field
$fieldId = $this->id . '_gmap3MarkerAddress';
echo CHtml::hiddenField('gmap3MarkerAddress', null, array('id' => $fieldId));
// options
$marker = (in_array('first', $options)) ? 'first:true' : 'last:true';
$pan = (in_array('nopan', $options)) ? '' : "$('#{$this->id}').gmap3({action:'panTo',args:[marker.position]});";
// construct marker position updater function
$setFunction = $this->id . 'setMarkerPosition';
$script = "function {$setFunction}(){
var addr = $('#{$fieldId}').val();
if ( !addr || !addr.length ) return;
marker = \$('#{$this->id}').gmap3({action:'get',name:'marker', {$marker}});
$('#{$this->id}').gmap3({
action: 'getlatlng',
address: addr,
callback: function(results){
if ( !results ) return;
latLng = results[0].geometry.location;
marker.setPosition(latLng);
{$pan}
}
});}";
// construct address field updater function
$upFunction = $this->id . 'updateAddressField';
$jsAttributes = CJavaScript::encode($attributes);
$modelClass = get_class($model);
$script .= "
function {$upFunction}() {
var add = '';
var fields = {$jsAttributes};
for (var i = 0; i < fields.length; i++) {
field = $('#'+fields[i]);
if (!field[0]){continue;}
if (field[0].type == 'select-one'){
value = $('#'+fields[i] +' :selected').text();
}
else {value = field.val();}
if (value && value != 0) {
add += value + ', ';
}
}
add = add.substring(0, add.length - 2);
$('#{$fieldId}').val(add);
{$setFunction}();
}";
if (isset($options['event'])) {
$eventType = $options['event'];
}
else {
$eventType = 'change';
}
$sAttributes = implode(',#', $attributes);
$script .= "$(document).delegate('#{$sAttributes}', '{$eventType}', function(){{$upFunction}();});";
$this->registerScript($script);
}
protected function registerScript($script, $position=CClientScript::POS_END)
{
$uid = md5($script);
$script = preg_replace('/[\n\r\t]/', null, $script);
Yii::app()->getClientScript()->registerScript($uid, $script, $position);
}
/**
* Used internally, should not be called.
*/
public function init()
{
$this->registerCoreScripts();
// action initialize
$script = $this->map->toJS();
// add objectified actions
$overlays = array(
'routes', 'markers', 'circles', 'rectangles', 'polygons', 'polylines',
'styledMaps',
);
foreach ($overlays as $actions) {
foreach ($this->$actions as $action) {
$script .= ',' . $action->toJs();
}
}
// other actions
if ($this->bicyclingLayer === true) {
$script .= ",{action:'addBicyclingLayer'}";
}
if ($this->trafficLayer === true) {
$script .= ",{action:'addTrafficLayer'}";
}
if ($this->autofit === true) {
$script .= ",{action:'autofit'}";
}
$script = "jQuery('#{$this->id}').gmap3($script)";
if ($this->resizable === true) {
$script .= '.resizable()';
}
$script .= ';';
// $script="jQuery('#yw0').gmap3({
// 'action': 'init',
// 'options': {
// 'center': ['41.850033', '-87.650052'],
// 'mapTypeControlOptions': {
// 'mapTypeIds': [google.maps.MapTypeId.ROADMAP, 'style1']
// },
// 'mapTypeId': google.maps.MapTypeId.ROADMAP,
// 'zoom': 12
// }
//}, {
// 'action': 'addStyledMap',
// 'id': 'style1',
// 'options': {
// 'name': 'style 1'
// },
// 'style': [{
// 'elementType': 'geometry',
// 'featureType': 'road.highway',
// 'stylers': [{
// 'hue': '#ff0022',
// 'saturation': 60,
// 'lightness': -20
// }]
// }]
//});";
//print_r($script);die;
$this->registerScript($script);
parent::init();
}
/**
* Used internally, should not be called.
*/
public function run()
{
echo CHtml::openTag('div', array(
'id' => $this->id,
'class' => 'gmap3',
'style' => "width:{$this->width}{$this->unit};height:{$this->height}{$this->unit}")),
CHtml::closeTag('div');
}
/**
* Attach all action objects to the map, convert them to Javascript,
* and render the map inside the widget.
*
* MUST be last method call.
*/
public function renderMap()
{
$this->init();
$this->run();
}
}