gpt4 book ai didi

javascript - 谷歌地图未显示在 Bootstrap 模式中

转载 作者:数据小太阳 更新时间:2023-10-29 03:59:44 24 4
gpt4 key购买 nike

我正计划在模态中显示谷歌地图,但 map 未从模态中显示

这是我的代码:

<div  style="margin-top:-7px;">
<button class="btn btn-default pull-right btn-mini " style="margin-right:5px;" data-toggle="modal" data-target="#myModal7"><i class="fa fa-globe"></i> Show Map</button>
</div>

<div class="modal inmodal fade" id="myModal7" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<h4 class="modal-title">Address</h4>
</div>
<div class="modal-body">
<div class="panel mb25 mt5" style="width:500px; margin:0 auto; padding:10px;">
<div id="map" style="width: 500px; height: 500px;"></div>
</div>
</div>

<div class="modal-footer">
<button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script type="text/javascript">
var address = 'Japan';
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 16
});
var geocoder = new google.maps.Geocoder();

geocoder.geocode({
'address': address
},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
google.maps.event.trigger(map, 'resize');
map.setCenter(results[0].geometry.location);
}
});
</script>

我对这个问题做了研究,我得到了共同的答案,它是 google.maps.event.trigger(map, "resize"); 问题是我要把它放在哪里,请帮助我。

谢谢

最佳答案

google map 与“display:none”父项有问题。

在第一次显示模态后初始化你的 map 。为显示模态的按钮或链接添加一个类。

$('.modal-opener-btn').one('click', function(){
//putt your code here
});

NOTICE : use "one" and dont use "on"

NOTICE : also can use modal "shown" listener, if this solution doesn't work

$('#myModal7').on('shown', function(){
//putt your code here
});

只需将底部脚本标签和内容更改为:

 <script type="text/javascript"> 
function init(){
var address = 'Japan';
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 16
});
var geocoder = new google.maps.Geocoder();

geocoder.geocode({
'address': address
},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
google.maps.event.trigger(map, 'resize');
map.setCenter(results[0].geometry.location);
}
});
}

$('#myModal7').on('shown.bs.modal', function(){
init();
});
</script>

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

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