refresh leaflet map: map container is already initialized

后端 未结 17 1051
鱼传尺愫
鱼传尺愫 2020-11-27 04:16

I have a page where given a select to the user he can switch the leaflet map I show.

After a initial leaflet map load, my problem is when i want to refresh the map.

17条回答
  •  -上瘾入骨i
    2020-11-27 04:26

    For refreshing map in same page you can use below code to create a map on the page

    if (!map) {
        this.map = new L.map("mapDiv", {
            center: [24.7136, 46.6753],
            zoom: 5,
            renderer: L.canvas(),
            attributionControl: true,
        });
    }
    

    then use below line to refresh the map, but make sure to use same latitude, longitude and zoom options

    map.setView([24.7136, 46.6753], 5);  
    

    Also, I had the same issue when switching between tabs in the same page using angular 2+, and I was able to fix it by adding below code in Component constructor

    var container = L.DomUtil.get('mapDiv');
    if (container != null) {
        container.outerHTML = ""; // Clear map generated HTML
        // container._leaflet_id = null; << didn't work for me
    }
    

提交回复
热议问题