2016-03-04 63 views
1

我有一個div容器內的單張地圖。它默認情況下不可見。 當我將鼠標懸停按鈕地圖變得可見。問題是,容器內的地圖瓦片沒有正確加載。此外,當我在地圖上放大它不會重新加載瓷磚正確。這似乎只有一個瓷磚加載。 有任何人的想法可能是什麼問題呢? 當我把地圖分別(沒有嵌套DIV容器)和可見默認它工作正常。單張顯示地圖 - 地圖不正確加載

var map = L.map('map'); 
 
var osm = L.tileLayer('http://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', { 
 
\t maxZoom: 19, 
 
\t attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, Tiles courtesy of <a href="http://hot.openstreetmap.org/" target="_blank">Humanitarian OpenStreetMap Team</a>' 
 
}); 
 

 
var geojsonFeature = [{ 
 
    "type": "Feature", 
 
    "properties": { 
 
\t \t "id": "marker1", 
 
     "name": "Coors Field" 
 
    }, 
 
    "geometry": { 
 
     "type": "Point", 
 
     "coordinates": [-104.99404, 39.75621] 
 
    } 
 
},{ 
 
    "type": "Feature", 
 
    "properties": { 
 
\t \t "id": "marker2", 
 
     "name": "School", 
 
    }, 
 
    "geometry": { 
 
     "type": "Point", 
 
     "coordinates": [-104.69404, 38.85621] 
 
    } 
 
}]; 
 

 
var markersById = {}; 
 

 
var markerLayer = L.geoJson(null, { 
 
    pointToLayer: function(feature, latlng) { 
 
    return L.marker(latlng, {}); 
 

 
    }, 
 
    onEachFeature: function(feature, layerinfo) { 
 
    if (feature.properties) { 
 
     var content = "<table class='table table-striped table-bordered table-condensed'>" + "<tr><th>Name</th><td>" + feature.properties.name + "<table>"; 
 

 
     layerinfo.bindPopup(content, { 
 
     closeButton: true 
 
     }); 
 

 
     // Save the layer into markersById if it has an id. 
 
     if (feature.properties.id) { 
 
     markersById[feature.properties.id] = layerinfo; 
 
     } 
 
    } 
 
    } 
 
}); 
 

 
markerLayer.addData(geojsonFeature); 
 
markerLayer.addTo(map); 
 
map.setView(markersById["marker1"].getLatLng(), 16); 
 
map.addLayer(osm);
body { 
 
    padding: 0; 
 
    margin: 0; 
 
} 
 

 
#map { 
 
    height: 100%; 
 
} 
 

 
.dropbtn { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
.mapContainer { 
 
    position: absolute; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
.mapContainer-content { 
 
    display: none; 
 
    position: absolute; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
.mapContainer:hover .mapContainer-content { 
 
    display: block; 
 
} 
 

 
.mapContainer:hover .dropbtn { 
 
    background-color: #3e8e41; 
 
}
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script> 
 
<link href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" rel="stylesheet"/> 
 
<body> 
 
    <div class="mapContainer"> 
 
    <button class="dropbtn">Map</button> 
 
    <div class="mapContainer-content"> 
 
     <div id="map"></div> 
 
    </div> 
 
    </div> 
 
</body>

回答

1

你必須使用map.invalidateSize()一旦你的地圖容器顯露。

檢查地圖容器大小是否更改並更新地圖,如果是這樣 - 在您動態更改地圖大小後調用它,默認情況下也動畫平移。

var map = L.map('map'); 
 
var osm = L.tileLayer('http://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', { 
 
\t maxZoom: 19, 
 
\t attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, Tiles courtesy of <a href="http://hot.openstreetmap.org/" target="_blank">Humanitarian OpenStreetMap Team</a>' 
 
}); 
 

 
var geojsonFeature = [{ 
 
    "type": "Feature", 
 
    "properties": { 
 
\t \t "id": "marker1", 
 
     "name": "Coors Field" 
 
    }, 
 
    "geometry": { 
 
     "type": "Point", 
 
     "coordinates": [-104.99404, 39.75621] 
 
    } 
 
},{ 
 
    "type": "Feature", 
 
    "properties": { 
 
\t \t "id": "marker2", 
 
     "name": "School", 
 
    }, 
 
    "geometry": { 
 
     "type": "Point", 
 
     "coordinates": [-104.69404, 38.85621] 
 
    } 
 
}]; 
 

 
var markersById = {}; 
 

 
var markerLayer = L.geoJson(null, { 
 
    pointToLayer: function(feature, latlng) { 
 
    return L.marker(latlng, {}); 
 

 
    }, 
 
    onEachFeature: function(feature, layerinfo) { 
 
    if (feature.properties) { 
 
     var content = "<table class='table table-striped table-bordered table-condensed'>" + "<tr><th>Name</th><td>" + feature.properties.name + "<table>"; 
 

 
     layerinfo.bindPopup(content, { 
 
     closeButton: true 
 
     }); 
 

 
     // Save the layer into markersById if it has an id. 
 
     if (feature.properties.id) { 
 
     markersById[feature.properties.id] = layerinfo; 
 
     } 
 
    } 
 
    } 
 
}); 
 

 
markerLayer.addData(geojsonFeature); 
 
markerLayer.addTo(map); 
 
map.setView(markersById["marker1"].getLatLng(), 16); 
 
map.addLayer(osm); 
 

 
document.getElementsByClassName("mapContainer")[0].addEventListener("mouseover", function() { 
 
    map.invalidateSize(); 
 
});
body { 
 
    padding: 0; 
 
    margin: 0; 
 
} 
 

 
#map { 
 
    height: 100%; 
 
} 
 

 
.dropbtn { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
.mapContainer { 
 
    position: absolute; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
.mapContainer-content { 
 
    display: none; 
 
    position: absolute; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
.mapContainer:hover .mapContainer-content { 
 
    display: block; 
 
} 
 

 
.mapContainer:hover .dropbtn { 
 
    background-color: #3e8e41; 
 
}
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script> 
 
<link href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" rel="stylesheet"/> 
 
<body> 
 
    <div class="mapContainer"> 
 
    <button class="dropbtn">Map</button> 
 
    <div class="mapContainer-content"> 
 
     <div id="map"></div> 
 
    </div> 
 
    </div> 
 
</body>

注意:如果需要的話,可以在超時包住map.invalidateSize(),讓一些時間瀏覽器通過CSS重新佈局:懸停時,它調用invalidateSize前以便它讀取正確的容器尺寸。