Find Leaflet map object after initialisation

筅森魡賤 提交于 2019-12-04 07:49:00

You can access the map from the global array created by the mediawiki extension.

Es: for accessing the first map of the page

window.maps.leafletList[0].map.getCenter()

For the record, in case you have the possibility to inject / execute some JS code before the map is initialized (i.e. before the "other script" executes), you can very easily customize Leaflet to keep a reference to each created maps.

E.g. using the addInitHook constructor hook on L.Map class:

// Before map is being initialized.
var mapsPlaceholder = [];

L.Map.addInitHook(function () {
  mapsPlaceholder.push(this); // Use whatever global scope variable you like.
});

// "Other script", can be in its own separate <script> and JS file.
L.map('mapId'); // The map object is pushed into `mapsPlaceholder` array.

// Then retrieve the map object to further manipulate the map.
var map = mapsPlaceholder.pop();

Demo: https://plnkr.co/edit/mywpSbfRPFOnJ8c1RNsZ?p=preview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!