tpa*_*age 3 leaflet mapbox bootstrap-modal twitter-bootstrap-3
使用 Leaflet 的 Mapbox 无法在模态中正确显示时遇到问题。
地图和标记位于左上角,其余图块为空白。
我已经直接在页面中尝试了代码而不是模式,它可以工作......
<style>
#mapid { height: 300px; }
</style>
<div id="mapid"></div>
<script>
var mymap = L.map('mapid').setView([<?= $return['latitude']; ?>, <?= $return['longitude']; ?>], 13);
//var mymap = L.Map('mapid', { center: new L.LatLng(<?= $return['latitude']; ?>, <?= $return['longitude']; ?>]), zoom: 15, layers: [nexrad], zoomControl: true });
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=<?= $MapBoxToken ?>', {
attribution: '© <a href="http://mapbox.com">Mapbox</a>, © <a href="http://openstreetmap.org">OpenStreetMap</a>',
maxZoom: 20,
id: 'mapbox.streets',
accessToken: '<?= $MapBoxToken ?>'
}).addTo(mymap);
var marker = L.marker([<?= $return['latitude']; ?>, <?= $return['longitude']; ?>]).addTo(mymap);
</script>
Run Code Online (Sandbox Code Playgroud)
如链接帖子中所述,在调整浏览器窗口大小时让地图工作只是问题的一个症状。Leaflet 侦听浏览器窗口调整大小事件,并在发生时再次读取地图视口尺寸,以便它可以正确显示图块并将视图居中。
在幕后,Leaflet 只是调用map.invalidateSize()resize 事件。
因此,针对您的情况的正确解决方案不是调整浏览器窗口的大小,而是调用map.invalidateSize().
并且不要在地图加载后执行此操作,而是在打开模态之后执行此操作,以便地图视口现在具有最终尺寸。
由于您在问题中添加了 Bootstrap 标签,我猜您的模态是使用 Bootstrap 构建的。在这种情况下,您可以收听"shown.bs.modal"事件:
var map = L.map('map').setView([48.86, 2.35], 11);
L.marker([48.86, 2.35]).addTo(map);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Comment out the below code to see the difference.
$('#myModal').on('shown.bs.modal', function() {
map.invalidateSize();
});Run Code Online (Sandbox Code Playgroud)
<!-- Bootstrap assets -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!-- Leaflet assets -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ==" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet-src.js" integrity="sha512-IkGU/uDhB9u9F8k+2OsA6XXoowIhOuQL1NTgNZHY1nkURnqEGlDZq3GsfmdJdKFe1k1zOc6YU2K7qY+hF9AodA==" crossorigin=""></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal that contains the map container.
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<!-- map container -->
<div id="map" style="height: 180px"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)