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 res
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.
this works for me
**<!-- Modal -->**
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Google Maps</h4>
</div>
<div class="modal-body">
<div id="map_canvas" style="width:auto; height: 500px;"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
**Button**
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">View Map</button>
**javascript**
<script type="text/javascript">
function initializeMap() {
var mapOptions = {
center: new google.maps.LatLng(51.219987, 4.396237),
zoom: 12,
mapTypeId: google.maps.MapTypeId.HYBRID
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(51.219987, 4.396237)
});
marker.setMap(map);
}
//show map on modal
$('#myModal').on('shown.bs.modal', function () {
initializeMap();
});
</script>
hope this will help
the following code work perfectly fine for bootstrap 3
$("#mapModal").on("shown.bs.modal", function () {initialize();});
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();
});
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
I wanted to point out a few modifications I made, based on the original answer, to make it work with Bootstrap 3 (check about modals usage).
// Resize map to show on a Bootstrap's modal
$('#map-modal').on('shown.bs.modal', function() {
var currentCenter = map.getCenter(); // Get current center before resizing
google.maps.event.trigger(map, "resize");
map.setCenter(currentCenter); // Re-set previous center
});
In this case, I also take care of recentering the map, based on this question suggested in Jan-Henk's comment to the first answer.