Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add google map in bootstrap modal

I am using Google Embed map option to insert a map into Bootstrap Modal, but the modal shows up with the shape of the map present, but only part of the map is displayed, the rest is gray.

Here is code i have used:

    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Click For Map</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

Here jquery i have attached

$('#myModal').on('shown.bs.modal', (function() {
  var mapIsAdded = false;

  return function() {
    if (!mapIsAdded) {
      $('.modal-body').html('<iframe src="https://www.google.com/maps/embed?pb=!1m14!1m12!1m3!1d9808.974038062652!2d4.3244048859985185!3d52.07529689519739!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!5e0!3m2!1snl!2snl!4v1419588821379" width="100%" height="400" frameborder="0" style="border:0"></iframe>');

      mapIsAdded = true;
    }    
  };
})());

I am not able find out the exact problem for this , any help would be greatly appreciated.

like image 243
Sandeep Siddanna Kumbar Avatar asked Nov 24 '25 07:11

Sandeep Siddanna Kumbar


1 Answers

It has to do with the modal being display: none initially. I think this makes the Google Maps JavaScript inside the iframe unable to work properly. To see for yourself try removing the .modal class and open the modal.

The easiest solution might be to inject the iframe when the modal is opened for the first time, using the shown.bs.modal event:

$('#myModal').on('shown.bs.modal', (function() {
  var mapIsAdded = false;

  return function() {
    if (!mapIsAdded) {
      $('.modal-body').html('<iframe src="https://www.google.com/maps/embed?pb=!1m14!1m12!1m3!1d9808.974038062652!2d4.3244048859985185!3d52.07529689519739!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!5e0!3m2!1snl!2snl!4v1419588821379" width="100%" height="400" frameborder="0" style="border:0"></iframe>');

      mapIsAdded = true;
    }    
  };
})());

or see this Codepen

Next to this the iframe has a fixed width="800" attribute. I would suggest you to change this into width="100%" as the modal window doesn't have a fixed width.

like image 192
ckuijjer Avatar answered Nov 25 '25 21:11

ckuijjer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!