Showing a Google Map in a modal created with Twitt

2019-01-07 03:57发布

I'm trying to insert a Google map into a modal using Twitter Bootstrap. The modal shows up with the shape of the map present, but only part of the map is displayed, the rest is gray. Upon resizing the screen the map always shows up correctly, although centered in the wrong place.

I've searched and found suggestions such as calling the map's resize event, or setting the max-width of the map image to none, but none of these suggestions have helped so far. It seems like the map is failing to figure out it's correct size as long as it's in an element that's hidden.

JS

function initialize() {
  var mapOptions = {
    center: new google.maps.LatLng(51.219987, 4.396237),
    zoom: 12,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("mapCanvas"),
    mapOptions);
  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(51.219987, 4.396237)
  });
  marker.setMap(map);
}

HTML

<div class="modal hide fade" id="myModal">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">×</button>
    <h3></h3>
  </div>
  <div class="modal-body">
    <div id="mapCanvas" style="width: 500px; height: 400px"></div>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Close</a>
  </div>
</div>

I'm using Twitter Bootstrap v2.0.4. I need some help because I'm failing to trigger the resize event correctly or editing the css so that the Google map is left alone.

11条回答
We Are One
2楼-- · 2019-01-07 04:29

The accepted answer got rid of the gray boxes, but does not center the map at the right coordinates for me. My solution was just to wait to render the map until after the modal is finished displaying.

$('#modal').on('shown', function () {
    initialize();
});
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-07 04:31

Google Maps indeed displays "Grey" area's when it's placed inside a dynamic element (for example: One that resizes, fades etc.). You're right about triggering the "resize" function, which should be called once the animation is complete (the shown.bs.modal event in Bootstrap 3 or the shown event in Bootstrap 2):

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

In Bootstrap 2, you will do:

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

(where map is the variable name of your map (see Google Maps documentation for further details), and #myModal the ID from your element).

UPDATE 2018-05-22

With a new renderer release in version 3.32 of Maps JavaScript API the resize event is no longer a part of Map class.

The documentation states

When the map is resized, the map center is fixed

  • The full-screen control now preserves center.

  • There is no longer any need to trigger the resize event manually.

source: https://developers.google.com/maps/documentation/javascript/new-renderer

google.maps.event.trigger(map, "resize"); doesn't have any effect starting from version 3.32

查看更多
乱世女痞
4楼-- · 2019-01-07 04:31

When using Bootstrap 3 you need to use what is provided within their documentation i.e. instead of 'shown' use 'shown.bs.modal'

Example:

 $('#modal').on('shown.bs.modal', function () { 
     initialiseMap();
 });
查看更多
Evening l夕情丶
5楼-- · 2019-01-07 04:34

I have fixed with solution:

$('#myId').on('shown.bs.modal', function () { 
    initializeMap() // with initializeMap function get map.
});

//event shown.bs.modal on modal bootstrap will show after modal show, not use show.bs.modal because it will call before modal show.

查看更多
可以哭但决不认输i
6楼-- · 2019-01-07 04:35

The accepted answer does, indeed, work in this case where the contents of the div are local. Unfortunately, I had the same problem showing a map that was included as part of remote data. Getting the handle of the map and calling resize did not correctly center my map. Here is how I fixed the issue in my remote data situation.

Include a script tag as part of your remote data with a function to initialize the map

<script type="text/javascript">
    function renderMap() {
        var point = new google.maps.LatLng(51.219987, 4.396237);
        var map = new google.maps.Map(document.getElementById('map'), {
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            zoom: 13,
            center: point
        });
        var marker = new google.maps.Marker({ position: point, map: map, icon: 'http://www.google.com/intl/en_us/mapfiles/ms/icons/red-dot.png' });
    }
</script>

Call the function in the shown event of the dialog on the main page

<script type="text/javascript">

    $(document).ready(function () {
        $('#dlgReportInfo').on('shown', function () {
            renderMap();
        });
    })
</script>

This way, the map is already sized in the dialog before you initialize it. Hopefully this will help out any others with a similar situation.

查看更多
一夜七次
7楼-- · 2019-01-07 04:44

the following code work perfectly fine for bootstrap 3

$("#mapModal").on("shown.bs.modal", function () {initialize();});
查看更多
登录 后发表回答