_center; } /** * Sets the center of the map. * * @param LatLng $value center of the map. */ public function setCenter(LatLng $value) { $this->_center = $value; } /** * @var Control[] holding controls to be added to the map. */ private $_controls = []; /** * @param \dosamigos\leaflet\controls\Control[] $controls * * @throws \yii\base\InvalidParamException */ public function setControls(array $controls) { foreach ($controls as $control) { if (!($control instanceof Control)) { throw new InvalidParamException("All controls must be of type Control."); } } $this->_controls = $controls; } /** * @return \dosamigos\leaflet\controls\Control[] */ public function getControls() { return $this->_controls; } /** * @param Control $control */ public function addControl(Control $control) { $this->_controls[] = $control; } /** * @var \dosamigos\leaflet\layers\TileLayer */ private $_tileLayer; /** * @param \dosamigos\leaflet\layers\TileLayer $tileLayer * * @return static the component itself */ public function setTileLayer(TileLayer $tileLayer) { if (!empty($tileLayer->map) && strcmp($tileLayer->map, $this->name) !== 0) { $this->name = $tileLayer->map; } if (empty($tileLayer->map)) { $tileLayer->map = $this->name; } $this->_tileLayer = $tileLayer; return $this; } /** * @return \dosamigos\leaflet\layers\TileLayer */ public function getTileLayer() { return $this->_tileLayer; } /** * @var array holds the js script lines to be registered. */ private $_js = []; /** * @param string|array $js custom javascript code to be registered. * *Warning*: This method overrides any previous settings. * * @return static the component itself */ public function setJs($js) { $this->_js = is_array($js) ? $js : [$js]; return $this; } /** * @param string $js appends javascript code to be registered. * * @return static the component itself */ public function appendJs($js) { $this->_js[] = $js; return $this; } /** * @return array the queued javascript code to be registered. * *Warning*: This method does not include map initialization. */ public function getJs() { $js = []; foreach ($this->getLayers() as $layer) { if ($layer instanceof Polygon) { $layerJs = $layer->encode(); $insertAtTheBottom = $layer->insertAtTheBottom ? 'true' : 'false'; $js[] = "$this->name.addLayer($layerJs, $insertAtTheBottom);"; continue; } $layer->map = $this->name; $js[] = $layer->encode(); } $groups = $this->getEncodedLayerGroups($this->getLayerGroups()); $controls = $this->getEncodedControls($this->getControls()); $plugins = $this->getEncodedPlugins($this->getPlugins()->getInstalledPlugins()); $js = ArrayHelper::merge($js, $groups); $js = ArrayHelper::merge($js, $controls); $js = ArrayHelper::merge($js, $plugins); $js = ArrayHelper::merge($js, $this->_js); return $js; } /** * @var PluginManager */ private $_plugins; /** * @return PluginManager */ public function getPlugins() { return $this->_plugins; } /** * Installs a plugin * * @param Plugin $plugin */ public function installPlugin(Plugin $plugin) { $plugin->map = $this->name; $this->getPlugins()->install($plugin); } /** * Removes an installed plugin * * @param $plugin * * @return mixed|null */ public function removePlugin($plugin) { return $this->getPlugins()->remove($plugin); } /** * Initializes the widget. */ public function init() { parent::init(); if (empty($this->center) || empty($this->zoom)) { throw new InvalidConfigException("'center' and/or 'zoom' attributes cannot be empty."); } $this->_plugins = new PluginManager(); $this->clientOptions['center'] = $this->center->toArray(true); $this->clientOptions['zoom'] = $this->zoom; } /** * Helper method to render the widget. It is also possible to use the widget directly: * ``` * echo Map::widget(['leafLet' => $leafLetObject, ...]); * ``` * * @param array $config * * @return string */ public function widget($config = []) { ob_start(); ob_implicit_flush(false); $config['leafLet'] = $this; $widget = new Map($config); $out = $widget->run(); return ob_get_clean() . $out; } /** * @param Layer $layer the layer script to add to the js script code. It could be any object extending from [[Layer]] * component (markers, polylines, popup, etc) * * @return static the component itself */ public function addLayer(Layer $layer) { $this->_layers[] = $layer; return $this; } /** * @return Layer[] the stored layers */ public function getLayers() { return $this->_layers; } /** * @param LayerGroup $group sets a layer group * * @return static the component itself */ public function addLayerGroup(LayerGroup $group) { $this->_layerGroups[] = $group; return $this; } /** * @return layers\LayerGroup[] all stored layer groups */ public function getLayerGroups() { return $this->_layerGroups; } /** * Clears all stored layer groups * @return static the component itself */ public function clearLayerGroups() { $this->_layerGroups = []; return $this; } /** * @param Control[] $controls * * @return array */ public function getEncodedControls($controls) { return $this->getEncodedObjects($controls); } /** * @param LayerGroup[] $groups * * @return array */ public function getEncodedLayerGroups($groups) { return $this->getEncodedObjects($groups); } /** * @param Plugin[] $plugins * * @return array */ public function getEncodedPlugins($plugins) { return $this->getEncodedObjects($plugins); } /** * @return string */ public static function generateName() { return self::$autoNamePrefix . self::$counter++; } /** * @param $objects * * @return array */ protected function getEncodedObjects($objects) { $js = []; foreach ((array)$objects as $object) { if (property_exists($object, 'map')) { $object->map = $this->name; } $js[] = method_exists($object, 'encode') ? $object->encode() : null; } return array_filter($js); } }