gpt4 book ai didi

javascript - 谷歌地图不显示

转载 作者:行者123 更新时间:2023-11-29 19:14:54 24 4
gpt4 key购买 nike

我正在尝试将 Google map 插入模式。模态显示了 map 的形状,但全是灰色的。我搜索并找到了一些建议,例如调用 map 的调整大小事件,或将 map 图像的最大宽度设置为无,但到目前为止这些建议都没有帮助。请帮助我。

JavaScript

var map;        
var myCenter=new google.maps.LatLng(53, -1.33);
var marker=new google.maps.Marker({
position:myCenter
});

function initialize() {
var mapProp = {
center:myCenter,
zoom: 14,
draggable: false,
scrollwheel: false,
mapTypeId:google.maps.MapTypeId.ROADMAP
};

map=new google.maps.Map(document.getElementById("map-canvas"),mapProp);
marker.setMap(map);

google.maps.event.addListener(marker, 'click', function() {

infowindow.setContent(contentString);
infowindow.open(map, marker);

});
};
google.maps.event.addDomListener(window, 'load', initialize);

google.maps.event.addDomListener(window, "resize", resizingMap());

$('#myMapModal').on('show.bs.modal', function() {
//Must wait until the render of the modal appear, thats why we use the resizeMap and NOT resizingMap!! ;-)
resizeMap();
})

function resizeMap() {
if(typeof map =="undefined") return;
setTimeout( function(){resizingMap();} , 400);
}

function resizingMap() {
if(typeof map =="undefined") return;
var center = map.getCenter();
google.maps.event.trigger(map, "resize");
map.setCenter(center);
}

html

<div class="modal fade" id="myMapModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modal title</h4>

</div>
<div class="modal-body">
<div class="container">
<div class="row">
<div id="map-canvas" class=""></div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

</div>

CSS

#map-canvas  {
margin: 0;
padding: 0;
height: 100%;
}

#map-canvas {
width:500px;
height:480px;
}

最佳答案

做模型时 map div 在启动时不可见并且创建不正确..我建议你在 resizeMap 函数上记忆一下 resizeMap 中的初始化函数

function resizeMap() {
initialize()
if(typeof map =="undefined") return;
setTimeout( function(){resizingMap();} , 400);
}

function resizingMap() {
initialize()
if(typeof map =="undefined") return;
var center = map.getCenter();
google.maps.event.trigger(map, "resize");
map.setCenter(center);
}

无论如何,当您打开模式时,您必须“初始化” map 。创建 map 时,包含 map 的 div 必须可见。

关于javascript - 谷歌地图不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36118979/

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