Modal bootstrap does not load Google Maps v3 correctly

Asked

Viewed 1,852 times

2

Imagem de erro

People I have the following problem, when loading Google Maps v3 in a Modal with the Bootstrap Framework the map is not displayed correctly.

Follows the code:

    <!DOCTYPE html>
     <html>
      <head>
        <meta charset="UTF-8">
        <link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
        <style>
            html, body, #map-canvas {
                height: 400px;
                margin: 0px;
                padding: 0px
            }
        </style>

    </head>
    <body>

        <a href="#" class="btn btn-default" id="openBtn">Open modal</a>

        <div class="modal fade" tabindex="-1" role="dialog" id="mapmodals">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                         <h4 class="modal-title">Peggy Guggenheim Collection - Venice</h4>

                    </div>
                    <div class="modal-body">
                        <div id="map-canvas"></div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="close" data-dismiss="modal">Fechar</button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>
        <!-- /.modal -->     

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>

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

            $("#openBtn").click(function () {
                $('#mapmodals').modal('show');
            });
        });

        var map;

        function initialize() {
            var mapOptions = {
                zoom: 8,
                center: new google.maps.LatLng(-34.397, 150.644)
            };

            map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        }

        google.maps.event.addDomListener(window, 'load', initialize);
        </script>

    </body>
</html>

Jsfiddle with example of error.

  • I built a Jsfiddle for you that reproduces the behavior: http://jsfiddle.net/h4d5006f/.

  • Wakim, in the Jsfiddle that you rode does not have the Modal.

  • I updated, see again, I forgot to give fork before.

  • Wakim, now it’s exactly like the image above that I posted, IE, with the error, the map does not render in the model. Know any possible solution ?

  • I edited your answer with a few adjustments, if I may. Includes the Jsfiddle link so you don’t have to search in the comments.

  • No problem. In the example you mounted in Jsfiddle if you select a place by Street View it renders and everything is right, makes the test.

  • I think the resize will work, but the event of shown.bs.modal is not being called.

Show 3 more comments

1 answer

3

The problem is I was running the bind before the DOM have been built in full. Soon the selector $('#mapmodals') was not bringing any element and so did not call the function set for the event, which resized the #map-canvas.

For this, I enclosed the initialization code of events within a ready jQuery.

The code stayed:

// Pode usar o document.ready também:
//$(document).ready(function() { ... });

$(function () {
    // Esse trecho de codigo sera executado quando a construção do DOM for terminada. Possibilitando a localização dos elementos nos seletores.
    $("#mapmodals").on('shown.bs.modal', function () {
        google.maps.event.trigger(map, 'resize');
    });
});

var map;

function initialize() {
    var mapOptions = {
        zoom: 8,
        center: new google.maps.LatLng(-34.397, 150.644)
    };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}

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

Example in Jsfiddle with the correction.

  • I copied like your example you solved, but I don’t know why you’re not showing anything now.

  • I found the possible error. You are running $('#mapmodals").on(...); before the document was loaded, soon the selector was empty. I will update my reply. Now it makes sense not to work.

  • I did like Jsfiddle and nothing yet.

  • If the solution did not work out you need more information about where it is applying. If you can put the rest of the html. This generating some error in the console?

  • I edited the question by inserting the whole page.

  • What version of Bootstrap this using? I made the JSFiddle with version 3.1.1. Do not skip importing the javascript file from Bootstrap?

  • I imported the Boostrap, look at the code.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.