Grey boxes appear in parts of embedded Google Map in modal box

*爱你&永不变心* 提交于 2019-11-27 07:00:40
gabeodess

Try using the modal event handlers. I think this is the most concise (and correct) way to ensure that you are resizing after the modal is shown without rewriting any plugins:

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

UPDATE: I just realized that this solution still does not center the map properly, so I needed to add one more command:

$('#map-modal').on('shown', function () {
  google.maps.event.trigger(map, 'resize');
  map.setCenter(new google.maps.LatLng(42.3605336, -72.6362989));
})
Johan Brook

I discovered that having display: none on the map's parent element (the modal) really messed things up. I changed it to visibility: hidden just in sheer desperation to see what would happen, and it worked!

I also modified the modal JS code to set the modal to visibility: visible/hidden when the modal is shown/hid. Otherwise it would get display: none/block set again, which would create the grey boxes again.

My solution for bootstrap-modal.js v 2.0

replace class "hide" for "invisible" in Modal div

<div id="map_modal" class="modal fade invisible">
...
</div>

function javascript

function open_map()
{   
   $('#map_modal').removeClass("invisible");
   $('#map_modal').modal('toggle');   
}

link html

<a href="javascript:open_map()">Open map</a>

For those using bootstrap, I had to remove "fade" from my "modal" class

From

<div class="modal fade" 

to

<div class="modal" ...

I had tried the previous solutions with calling google.maps.event.trigger(map, 'resize'); but figured that

.on("shown.bs.modal" ...

was never getting called. This issue seemed to point me to removing the fade.
https://github.com/twbs/bootstrap/issues/11793

This is not an optimal solution since the fade is actually very nice to have...

The problem is that if you do something like this:

$("#mapModalLink").click(function(){
  google.maps.event.trigger(map,"resize");
});

is that your modal probably wasn't open (and displayed at the right size) when the resize event is triggered. To solve this:

$("#mapModalLink").click(function(){
  $("#mapModal").show();
  google.maps.event.trigger(map, 'resize');
});

Worked for me at least :)

I had this problem and I solved it doing a callback to the draw method. I think this happens because you draw your map erlier than showing the dialog.

I used this code to solve it (it wasn´t a modal window):

$('#map').show(function()
{
    var punto = new google.maps.LatLng(this.latitud, this.longitud);
    var myOptions = {zoom: 15, center: punto, mapTypeId: google.maps.MapTypeId.ROADMAP,mapTypeControl:false, draggable:false};
    var map = new google.maps.Map(document.getElementById('mapa'),  myOptions);
    var marker = new google.maps.Marker({
    position:punto,
    map: map,
    title: this.nombre
});
});
google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
     google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
          google.maps.event.trigger(map, 'resize');

     });
});

As far as I poked at it a bit on Chrome using JS live edit, delaying the execution of resize to wait for the modal animation might work :)

https://gist.github.com/1592330#L107

map rendered successfully without resize.

Why resize gives you proper map ?

Because browser paints the view again.

How to fix it?

To get a proper layout of the map in any panel which is triggered lately, map has to be painted after the panel is loaded.This can be achieved by (setTimeout) code as mentioned below. code objective is to trigger map resize event after 60 milli seconds.

setTimeout(function () {
        uiGmapIsReady.promise().then(function (maps) {
            google.maps.event.trigger(maps[0].map, 'resize');
            lat = -37;
            lon = 144;
            maps[0].map.panTo(new google.maps.LatLng(lat,lon));
            var marker = {
                id: Date.now(),
                coords: {
                    latitude: lat,
                    longitude: lon
                }
            };
            $scope.map.markers = [];
            $scope.map.markers.push(marker);
            console.log($scope.map.markers);
        });
    }, 60);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!