refresh leaflet map: map container is already initialized

后端 未结 17 890
鱼传尺愫
鱼传尺愫 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:33

    I had same problem.then i set globally map variable e.g var map= null and then for display map i check

    if(map==null)then map=new L.Map('idopenstreet').setView();
    

    By this solution your map will be initialize only first time after that map will be fill by L.Map then it will not be null. so no error will be there like map container already initialize.

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

    if you want update map view, for example change map center, you don’t have to delete and then recreate the map, you can just update coordinate

    const mapInit = () => {
     let map.current = w.L.map('map');
    
     L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm.org/copyright" target="_blank">OpenStreetMap</a> contributors'
     }).addTo(map.current);
    }
    
    const setCoordinate = (gps_lat, gps_long) => {
      map.setView([gps_lat, gps_long], 13);
    }
    
    initMap();
    
    setCoordinate(50.403723 30.623538);
    
    setTimeout(() => {
      setCoordinate(51.505, -0.09);
    }, 3000);
    
    0 讨论(0)
  • 2020-11-27 04:36

    We facing this issue today and we solved it. what we do ?

    leaflet map load div is below.

    <div id="map_container">
       <div id="listing_map" class="right_listing"></div>
    </div>
    

    When form input change or submit we follow this step below. after leaflet map container removed in my page and create new again.

    $( '#map_container' ).html( ' ' ).append( '<div id="listing_map" class="right_listing"></div>' );
    

    After this code my leaflet map is working fine with form filter to reload again.

    Thank you.

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

    What you can try is to remove the map before initialising it or when you leave the page:

    if(this.map) {
      this.map.remove();
    }
    
    0 讨论(0)
  • 2020-11-27 04:40

    use the redrawAll() function rather than renderAll().

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

    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;
    
    }
    

    It works for me

    0 讨论(0)
提交回复
热议问题