简体   繁体   English

Leaflet Map未显示在bootstrap div中

[英]Leaflet Map not showing in bootstrap div

This is a super odd problem that I can not figure out. 这是一个我无法弄清楚的超奇怪的问题。 I have my div with the map and its css styling. 我有我的div与地图和它的CSS风格。 The Leaflet map is showing up in one rectangle and not in the div. Leaflet地图显示在一个矩形中,而不是div中。 Its as if the z index is wrong, but I can not figure it out. 它好像z索引是错误的,但我无法弄明白。 The JS is in the document.ready function. JS位于document.ready函数中。 I added a border around the map div just to show how off everything is. 我在地图div周围添加了一个边框,以显示一切都是如何。 在此输入图像描述

CSS: CSS:

#map {width:100%;height:100%;margin-left:10px;margin-top:40px}

HTML: HTML:

  <div id="showTravel" class="row" style="display:none">
        <div class="col-lg-12">
            <div class="wrapper wrapper-content">
        <h2 style="margin-top:-18px">All Travelers</h2>
        <div id="travelContent">
            <div id=map></div>
        </div>
            </div>
        </div>
    </div>

JS: JS:

var map=L.map('map',{
        minZoom:2,
    maxZoom:18
}).setView([x,y],16);

var buildingIcon=L.icon({
    iconUrl:'/images/bicon.png',
    iconSize:[25,40],
    popupAnchor: [-3, -20],
    iconAnchor: [14, 38]
});

L.marker(x,y],{
    icon:buildingIcon,
    title:'town, state'
})
.bindPopup('<b>first last</b><br>Email: email address<br>Cell: phone number')
.addTo(map);

mapLink='<a href="http://openstreetmap.org">OpenStreetMap</a>';

L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: 'Map data &copy; ' + mapLink,
    maxZoom:18
}).addTo(map);

I have fixed the overlapping problem , but the last problem still exists ie, 我已经解决了重叠问题 ,但最后一个问题仍然存在,即

When the page loads and the map is rendered on the document.ready() the map is only visible in the top left corner of the screen. 当页面加载并且在document.ready()上呈现地图时,地图仅在屏幕的左上角可见。 And If I changes the size of the browser (maximize,minimize) then the map refreshes correctly. 如果我更改浏览器的大小(最大化,最小化),则地图会正确刷新。

Execute the following function once the map is opened: 打开地图后执行以下功能:

map.invalidateSize();

This will fix your problem. 这将解决您的问题。

I used it with hided divs, which appear after a click. 我用它来隐藏div,点击后出现。 The tiles were also not visible, so I added map.invalidateSize() to the jQuery function where the div is set visible. 瓦片也是不可见的,所以我将map.invalidateSize()添加到jQuery函数中,其中div设置为可见。 Hope that helps... 希望有帮助......

This worked for me, 这对我有用,

$('#showTravel').on('shown.bs.modal', function (e) {
  //creation of map
})

Or search in event of showing the div and into function creating the map 或者在显示div并进入创建地图的功能时进行搜索

I have the same issue to display map inside Bootstrap Tabs. 我在Bootstrap选项卡中显示地图有同样的问题。 I have fixed it by using - 我用以下方法修复了它

 $('#gmap').on('shown.bs.tab', function (e) {
    //clear map first
    clearMap();
    //resize the map
   map.invalidateSize(true);
   //load the map once all layers cleared
   loadMap();
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM