refresh leaflet map: map container is already initialized

后端 未结 17 889
鱼传尺愫
鱼传尺愫 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条回答
  • 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
    }
    
    0 讨论(0)
  • 2020-11-27 04:27

    Html:

    <div id="weathermap"></div>
    

    JavaScript:

    function buildMap(lat,lon)  {
        document.getElementById('weathermap').innerHTML = "<div id='map' style='width: 100%; height: 100%;'></div>";
        var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
                        osmAttribution = 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors,' +
                            ' <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
        osmLayer = new L.TileLayer(osmUrl, {maxZoom: 18, attribution: osmAttribution});
        var map = new L.Map('map');
        map.setView(new L.LatLng(lat,lon), 9 );
        map.addLayer(osmLayer);
        var validatorsLayer = new OsmJs.Weather.LeafletLayer({lang: 'en'});
        map.addLayer(validatorsLayer);
    }
    

    I use this:

    document.getElementById('weathermap').innerHTML = "<div id='map' style='width: 100%; height: 100%;'></div>";
    

    to reload content of div where render map.

    0 讨论(0)
  • 2020-11-27 04:28

    I had the same problem on angular when switching page. I had to add this code before leaving the page to make it works:

        $scope.$on('$locationChangeStart', function( event ) {
        if(map != undefined)
        {
          map.remove();
          map = undefined
          document.getElementById('mapLayer').innerHTML = "";
        }
    });
    

    Without document.getElementById('mapLayer').innerHTML = "" the map was not displayed on the next page.

    0 讨论(0)
  • 2020-11-27 04:31

    Before initializing map check for is the map is already initiated or not

    var container = L.DomUtil.get('map');
          if(container != null){
            container._leaflet_id = null;
          }
    
    0 讨论(0)
  • 2020-11-27 04:31

    Only use this

    map.invalidateSize();
    

    https://github.com/Leaflet/Leaflet/issues/690

    0 讨论(0)
  • 2020-11-27 04:31

    When you just remove a map, it destroys the div id reference, so, after remove() you need to build again the div where the map will be displayed, in order to avoid the "Uncaught Error: Map container not found".

    if(map != undefined || map != null){
        map.remove();
       $("#map").html("");
       $("#preMap").empty();
       $( "<div id=\"map\" style=\"height: 500px;\"></div>" ).appendTo("#preMap");
    }
    
    0 讨论(0)
提交回复
热议问题