简体   繁体   中英

Google map not showing on bootstrap modal

Im am planning to show a google map on a modal, but the map is not showing from the modal

This is my code:

<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>

I did a research for this problem, ang i got the common answer, it is google.maps.event.trigger(map, "resize"); the problem is where will i put it, please help me.

Thanks

google map has problem with "display:none" parent.

initialize your map after showing modal for first time. add a class for button or link that show modal.

$('.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
});

Just change bottom script tag and contents to :

 <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>

regarding your research you can try to use that function

inside this

$(document).ready(function(){

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

});

有同样的问题,这是我的一天的解决方案:确保你在调用你的模态页面的初始页面上有jQuery:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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