yii2-leaflet-extension icon indicating copy to clipboard operation
yii2-leaflet-extension copied to clipboard

Wrong initialization scrip of the map

Open bobonov opened this issue 7 years ago • 2 comments

Since the map is initialized in the following way:

function map_init(){
    var map = L.map('w0', {});

this make map unavailable in global scope therefore is impossible to operate via javascript on the map (ie change the center) Something, at javascript level, like this should work.

var map;
function map_init(){
    map = L.map('w0', {});

So in the php code in Map.php: line 103 from

array_unshift($js, "var $name = L.map('$id', $options);");

to

array_unshift($js, "$name = L.map('$id', $options);");

and line 122

$view->registerJs("function {$name}_init(){\n" . implode("\n", $js) . "}\n{$name}_init();");

to

$view->registerJs("var {$name};\nfunction {$name}_init(){\n" . implode("\n", $js) . "}\n{$name}_init();");

bobonov avatar Feb 07 '18 16:02 bobonov

I made a test and is sufficient to change line 103 in this way the map var is implicitly global.

bobonov avatar Feb 07 '18 16:02 bobonov

I think layers/* (I checked only some of them) suffer from same issue. In the files I checked is sufficient to remove var in front of the javascript variable declaration. form

$js = "var $name = $js;"

to

$js = "$name = $js;"

bobonov avatar Feb 07 '18 16:02 bobonov