refresh leaflet map: map container is already initialized

后端 未结 17 1108
鱼传尺愫
鱼传尺愫 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: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: '© OpenStreetMap 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);
    

提交回复
热议问题