使用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>
答案 0 :(得分:1)
正如linked post中所述,在调整浏览器窗口大小时使地图工作只是问题的症状。 Leaflet侦听浏览器窗口调整大小事件,并在发生时再次读取地图视口尺寸,以便它可以正确显示切片并使视图居中。
简介,Leaflet只需在调整大小事件上调用map.invalidateSize()
。
因此,针对您的案例的正确解决方案是不,以便调整浏览器窗口的大小,而是调用map.invalidateSize()
。
在地图加载后执行 NOT ,但在打开模态后,以便地图视口现在具有最终尺寸。
由于您在问题中添加了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();
});
<!-- 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>
答案 1 :(得分:0)
问题似乎与bootstap模式有关。
需要添加map.invalidateSize();但根据ghybs的建议,在&#34; shown.bs.modal&#34;内。
$('#myModal').on('shown.bs.modal', function() {
map.invalidateSize();
});
请注意&#34;地图&#34;在map.invalidateSize();需要替换为&#34; var map = L.map()&#34;中指定的可变量。我在这篇文章的原始代码中是:
var mymap = L.map('mapid').setView([<?= $return['latitude']; ?>, <?= $return['longitude']; ?>], 13);
所以我补充说:
$('#modalID').on('shown.bs.modal', function() {
mymap.invalidateSize();
});