我的页面中有一个嵌套在隐藏的 div 中的 Google 地图。我点击链接后显示该 div,但是只有地图的左上角显示出来。
我尝试在点击后运行如下代码:
map0.onResize();
或者:google.maps.event.trigger(map0, 'resize')
我该怎么做?
这是一个显示了隐藏地图的div后所看到的图片:
我的页面中有一个嵌套在隐藏的 div 中的 Google 地图。我点击链接后显示该 div,但是只有地图的左上角显示出来。
我尝试在点击后运行如下代码:
map0.onResize();
或者:google.maps.event.trigger(map0, 'resize')
我该怎么做?
这是一个显示了隐藏地图的div后所看到的图片:
我遇到了相同的问题,并发现当显示div时,调用google.maps.event.trigger(map, 'resize');
似乎解决了我的问题。
我刚刚亲自测试了一下,以下是我的做法。这很简单明了。
<div id="map_canvas" style="width:700px; height:500px; margin-left:80px;" ></div>
<button onclick="displayMap()">Show Map</button>
<style type="text/css">
#map_canvas {display: none;}
</style>
<script>
function displayMap()
{
document.getElementById( 'map_canvas' ).style.display = "block";
initialize();
}
function initialize()
{
// Create the map
var myOptions = {
zoom: 14,
center: new google.maps.LatLng( 0.0, 0.0 ),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map( document.getElementById( "map_canvas" ), myOptions );
}
</script>
使用:
google.maps.event.trigger($("#div_ID")[0], 'resize');
如果您没有可用的变量映射,它应该是包含GMAP的
我在Bootstrap选项卡中嵌入了一个Google地图,但它显示不正确。这是我的解决方法。
// Previously stored my map object(s) in googleMaps array
$('a[href="#profileTab"]').on('shown', function() { // When tab is displayed...
var map = googleMaps[0],
center = map.getCenter();
google.maps.event.trigger(map, 'resize'); // fixes map display
map.setCenter(center); // centers map correctly
});
'a[href="#profileTab"]'
更改为step-2
,它是包含地图DIV的DIV的名称。 - GeospatialPython.com仅仅调用 google.maps.event.trigger(map, 'resize');
是不够的。您还应该重设地图的中心。
var map;
var initialize= function (){
...
}
var resize = function () {
if (typeof(map) == "undefined") {) {
// initialize the map. You only need this if you may not have initialized your map when resize() is called.
initialize();
} else {
// okay, we've got a map and we need to resize it
var center = map.getCenter();
google.maps.event.trigger(map, 'resize');
map.setCenter(center);
}
}
Angular (ng-show 或 ui-bootstrap 折叠组件)
直接绑定元素的可见性,而不是绑定到 ng-show 的值,因为 $watch 可能会在 ng-show 更新之前触发(导致 div 仍然是不可见的)。
scope.$watch(function () { return element.is(':visible'); },
function () {
resize();
}
);
jQuery .show()
使用内置的回调函数
$("#myMapDiv").show(speed, function() { resize(); });
Bootstrap 3 模态框
$('#myModal').on('shown.bs.modal', function() {
resize();
})
还有一种方法是直接触发本地窗口大小调整事件。
Google Maps 将自动重新加载:
window.dispatchEvent(new Event('resize'));
document.getElementById("map-wrapper").innerHTML = document.getElementById("map-wrapper").innerHTML;
jQuery版本:
$('#map-wrapper').html( $('#map-wrapper').html() );
HTML:
<!-- .... -->
<div id="map-wrapper"><iframe src="https://www.google.com/maps/..." /></div>
<!-- .... -->
<script>
$(document).ready( function() {
/* Detects when the tab is selected */
$('a[href="#tab-id"]').on('shown.bs.tab', function() {
/* When the tab is shown the content of the
wrapper is regenerated and reloaded */
$('#map-wrapper').html( $('#map-wrapper').html() );
});
});
</script>
https://www.google.com/maps/embed/v1/place?..
的 iframe 的人来说,这是最佳解决方案。 - gsinha我曾经遇到过同样的问题,但是使用 google.maps.event.trigger(map, 'resize')
并没有解决。
我添加了一个定时器,在将 div
可见后更新地图...
// Working code
var refreshIntervalId;
function showMap() {
document.getElementById('divMap').style.display = '';
refreshIntervalId = setInterval(function () { updateMapTimer() }, 300);
}
function updateMapTimer() {
clearInterval(refreshIntervalId);
var map = new google.maps.Map(....
....
}
我不知道这是否是更方便的方法,但它可以正常工作!
function waitForVisibleMapElement() {
setTimeout(function () {
if ($('#map_canvas').is(":visible")) {
// Initialize your Google Map here
} else {
waitForVisibleMapElement();
};
}, 100);
};
waitForVisibleMapElement();
<div class="map-wrap">
<div id="map-canvas"></div>
</div>
.map-wrap{
height: 0;
width: 0;
overflow: hidden;
}
$('.map-wrap').css({ height: 'auto', width: 'auto' }); // For showing your map
$('.map-wrap').css({ height: 0, width: 0 }); // For hiding your map