Skip to content

Instantly share code, notes, and snippets.

@gfargo
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfargo/4bfb7268c77d94ab7dd3 to your computer and use it in GitHub Desktop.
Save gfargo/4bfb7268c77d94ab7dd3 to your computer and use it in GitHub Desktop.
Extend 2amigos Leaflet Map Class - Allowing for Global access to Map variable
<?php
namespace app\components;
use dosamigos\leaflet\LeafLet;
use dosamigos\leaflet\widgets\Map;
use dosamigos\leaflet\LeafLetAsset;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
class CustomMapWidget extends Map
{
public function registerScript()
{
$view = $this->getView();
LeafLetAsset::register($view);
$this->leafLet->getPlugins()->registerAssetBundles($view);
$id = $this->options['id'];
$name = $this->leafLet->name;
$js = $this->leafLet->getJs();
$clientOptions = $this->leafLet->clientOptions;
$options = empty($clientOptions) ? '{}' : Json::encode($clientOptions);
array_unshift($js, "$name = L.map('$id', $options);");
if ($this->leafLet->getTileLayer() !== null) {
$js[] = $this->leafLet->getTileLayer()->encode();
}
$clientEvents = $this->leafLet->clientEvents;
if (!empty($clientEvents)) {
foreach ($clientEvents as $event => $handler) {
$js[] = "$name.on('$event', $handler);";
}
}
$view->registerJs("function {$name}_init(){\n" . implode("\n", $js) . "}\n{$name}_init();");
}
}
<?php
// first lets setup the center of our map
$center = new dosamigos\leaflet\types\LatLng(['lat' => 51.508, 'lng' => -0.11]);
// now lets create a marker that we are going to place on our map
$marker = new \dosamigos\leaflet\layers\Marker(['latLng' => $center, 'popupContent' => 'Hi!']);
// The Tile Layer (very important)
$tileLayer = new \dosamigos\leaflet\layers\TileLayer([
'urlTemplate' => 'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png',
]);
// now our component and we are going to configure it
$leaflet = new \dosamigos\leaflet\LeafLet([
'center' => $center, // set the center
]);
// Different layers can be added to our map using the `addLayer` function.
$leaflet->addLayer($marker) // add the marker
->addLayer($tileLayer); // add the tile layer
// Render Custom Map Widget
echo \app\components\CustomMapWidget::widget([
'leafLet' => $leaflet,
'id' => 'ww_map',
'height' => '400',
]);
?>
<?php
namespace app\components\widgets;
use dosamigos\leaflet\LeafLet;
use dosamigos\leaflet\widgets\Map;
use dosamigos\leaflet\LeafLetAsset;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
class WWMap extends Map
{
public function registerScript()
{
$view = $this->getView();
LeafLetAsset::register($view);
$this->leafLet->getPlugins()->registerAssetBundles($view);
$id = $this->options['id'];
$name = $this->leafLet->name;
$js = $this->leafLet->getJs();
$clientOptions = $this->leafLet->clientOptions;
$options = empty($clientOptions) ? '{}' : Json::encode($clientOptions);
array_unshift($js, "$name = L.map('$id', $options);");
if ($this->leafLet->getTileLayer() !== null) {
$js[] = $this->leafLet->getTileLayer()->encode();
}
$clientEvents = $this->leafLet->clientEvents;
if (!empty($clientEvents)) {
foreach ($clientEvents as $event => $handler) {
$js[] = "$name.on('$event', $handler);";
}
}
$view->registerJs("function {$name}_init(){\n" . implode("\n", $js) . "}\n{$name}_init();");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment