【问题标题】:Google map not showing on bootstrap modal谷歌地图未显示在引导模式上
【发布时间】:2015-08-16 16:13:57
【问题描述】:

我正计划在模式中显示谷歌地图,但地图没有从模式中显示

这是我的代码:

<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"); 问题是我将它放在哪里,请帮助我。

谢谢

【问题讨论】:

    标签: javascript twitter-bootstrap google-maps


    【解决方案1】:

    谷歌地图与“显示:无”父级存在问题。

    在第一次显示模式后初始化您的地图。 为显示模式的按钮或链接添加一个类。

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

    注意:使用“one”,不要使用“on”

    注意:如果此解决方案不起作用,也可以使用模态“显示”侦听器

    $('#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>
    

    【讨论】:

      【解决方案2】:

      关于您的研究,您可以尝试使用该功能

      里面

      $(document).ready(function(){
      
      $('#your_modal_id').on('shown.bs.modal',function(){
        google.maps.event.trigger(map, "resize");
      });
      
      });
      

      【讨论】:

      • 这个可以了,但是marker不见了,怎么把marker放回去?
      • 仔细观察后,我认为您可以将我的解决方案与之前的解决方案结合起来。我的意思是用调用 init() 函数替换 google.maps.event.trigger(map,'resize')
      • 我只是将您的代码添加到脚本的底部,它现在正在工作并且地图正在显示,但标记不见了
      • 你是否用 Bojbaj 的脚本更改了最后一个脚本?
      【解决方案3】:

      遇到了同样的问题,这就是让我开心的解决方案: 确保在调用模态页面的初始页面上有 jQuery:

      【讨论】:

        猜你喜欢
        • 2023-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多