-
Notifications
You must be signed in to change notification settings - Fork 2
/
Map.php
106 lines (90 loc) · 3.01 KB
/
Map.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
<?php
namespace edofre\markerclusterer;
use dosamigos\google\maps\ObjectAbstract;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\JsExpression;
/**
* Class Map
* @package edofre\markerclusterer
*/
class Map extends \dosamigos\google\maps\Map
{
/**
* @var array the JS options for the MarkerClusterer object
*/
public $clusterOptions = [];
/**
* @var array stores javascript code that is going to be rendered together with script initialization
*/
private $_js = [];
/**
* @return string
*/
public function getJs()
{
$name = $this->getName();
$width = strpos($this->width, "%") ? $this->width : $this->width . 'px';
$height = strpos($this->height, "%") ? $this->height : $this->height . 'px';
$containerId = ArrayHelper::getValue($this->containerOptions, 'id', $name . '-map-canvas');
$overlaysJs = [];
$js = [];
foreach ($this->getOverlays() as $overlay) {
/** @var ObjectAbstract $overlay */
if (!ArrayHelper::keyExists("{$name}infoWindow", $this->getClosureScopedVariables()) &&
method_exists($overlay, 'getIsInfoWindowShared')
&& $overlay->getIsInfoWindowShared()
) {
$this->setClosureScopedVariable("{$name}infoWindow");
$this->appendScript("{$name}infoWindow = new google.maps.InfoWindow();");
}
$overlay->options['map'] = new JsExpression($this->getName());
$overlaysJs[] = $overlay->getJs();
}
$js[] = "(function(){";
$js[] = $this->getClosureScopedVariablesScript();
$js[] = "function initialize(){";
$js[] = "var mapOptions = {$this->getEncodedOptions()};";
$js[] = "var markers = [];";
$js[] = "var container = document.getElementById('{$containerId}');";
$js[] = "container.style.width = '{$width}';";
$js[] = "container.style.height = '{$height}';";
$js[] = "var {$this->getName()} = new google.maps.Map(container, mapOptions);";
$js = ArrayHelper::merge($js, $overlaysJs);
// Make object available globally
$js[] = "window.globalmap = {$this->getName()};";
foreach ($this->events as $event) {
/** @var Event $event */
$js[] = $event->getJs($name);
}
foreach ($this->getPlugins()->getInstalledPlugins() as $plugin) {
/** @var \dosamigos\google\maps\PluginAbstract $plugin */
$plugin->map = $this->getName();
$js[] = $plugin->getJs($name);
}
$js = ArrayHelper::merge($js, $this->_js);
// Register the ClustererAsset
$view = \Yii::$app->getView();
$cluster_asset_manager = ClustererAsset::register($view);
$this->clusterOptions = ArrayHelper::merge([
'imagePath' => $cluster_asset_manager->imagePath,
], $this->clusterOptions);
$jsonClusterOptions = Json::encode($this->clusterOptions);
// Create the MarkerClusterer object and add the markers
$js[] = "var markerCluster = new MarkerClusterer({$name}, markers, $jsonClusterOptions);";
$js[] = "};";
$js[] = "google.maps.event.addDomListener(window, 'load', initialize);";
$js[] = "})();";
return implode("\n", $js);
}
/**
* @param $js
*
* @return $this
*/
public function appendScript($js)
{
$this->_js[] = $js;
return $this;
}
}