gpt4 book ai didi

javascript - LeafLet map 无法在模态上正确呈现

转载 作者:行者123 更新时间:2023-11-30 11:35:17 24 4
gpt4 key购买 nike

我正在做一个大学项目,我对 JS 编码很陌生,我遇到了一个问题,我正在使用 w3 库实现我的应用程序,当用户单击按钮时会显示一个模态窗口,这个模态包含一张无法正确呈现的 Leaflet map ,因此我在 Stackoverflow 上找到了解决此问题的方法: Leaflet map not showing properly in bootstrap 3.0 modal所以我在该线程中尝试了建议的解决方案并将其放入我的 JS 代码中:

$('#comparator_modal').on('show.w3.modal', function(){
setTimeout(function() {
mymap.invalidateSize();
}, 400);
});

但我遇到了另一个我找不到解决方案的问题。我的 map 仍然呈现得很糟糕,但是当我调整浏览器窗口大小时它似乎工作正常(重新呈现整个 map ,并正确显示 map ),所以我认为这个问题可能与时间有关,因为模态可能需要很长时间才能加载和map invalidateSize() 在加载模态之前执行,我尝试使用不同的超时(4、40、400、4000),但这没有解决任何问题,所以我有点迷失在这里,任何想法都会受到赞赏,伙计们这是我的模态主体:

<div id="comparator_modal" class="  w3-modal">
<div class="w3-modal-content w3-animate-left w3-card-4">
<header class="w3-container w3-blue">
<span onclick="document.getElementById('comparator_modal').style.display='none'"
class="w3-btn w3-red w3-round w3-display-topright" style="font-size:16px; text-shadow:2px 1px 0 #444;">&times;</span>
<h3 class="w3-center" style="text-shadow:2px 1px 0 #444;">Comparator</h3>
</header>
<body>
<div id="comparator_modal_body">
<div class=w3-row">
<div id="map01_comparator"></div>
<!--<div id=map01_comparator class="w3-half w3-text-black">Map#1</div>
<div id=map02_comparator class="w3-half w3-text-black">Map#2</div>-->
<div class="w3-row">
<div class="w3-half w3-center w3-text-black">
<div id="time_stamp1">Time_stamp</div>
</div>
<div class="w3-half w3-center w3-text-black">
<div id = "time_stamp2">Time_stamp</div>
</div>
</div>
</div>
</div>
</body>
<footer class="w3-container w3-light-grey">

</footer>
</div>
</div>

还有我加载 map 的 JS:

<head>
<!-- Load Leaflet from CDN-->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" >
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
<!-- Load Esri Leaflet from CDN -->
<script src="https://unpkg.com/esri-leaflet@2.0.8/dist/esri-leaflet.js"></script>

<style>
#basemaps-wrapper {
position: absolute;
top: 10px;
right: 10px;
z-index: 900;
background: white;
padding: 10px;
}
#basemaps {
margin-bottom: 5px;
}
</style>
</head>

<script language="javascript" type="text/javascript">

// initialize the map
var mymap = L.map('map01_comparator').setView([42.35, -71.08], 13);
// load a tile layer
L.tileLayer('http://tiles.mapc.org/basemap/{z}/{x}/{y}.png',
{
attribution: 'Tiles by <a href="http://mapc.org">MAPC</a>, Data by <a href="http://mass.gov/mgis">MassGIS</a>',
maxZoom: 17,
minZoom: 9
}).addTo(mymap);

$('#comparator_modal').on('show.w3.modal', function(){
setTimeout(function() {
mymap.invalidateSize();
}, 4000);
});

</script>

最佳答案

W3CSS 是一个纯 CSS 框架,因此它没有像 bootstrap modal 这样的事件。您需要在用于打开模式的函数中使 map 的大小无效:

document.getElementById("mybutton").onclick = function () {
document.getElementById('mymodal').style.display = 'block';
setTimeout(function() {
mymap.invalidateSize();
}, 100);
}

关于javascript - LeafLet map 无法在模态上正确呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44737760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com