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
For me, it works like this (Boostrap 3):
$("#contact-modal").on("shown.bs.modal", function () {
google.maps.event.trigger(map, "resize");
});
I am also facing the same issue but i resolve it by waiting for the map's 'idle' state (i.e. when it's finished) and then calling the resize:
google.maps.event.addListenerOnce(map, 'idle', function () {
var currentCenter = map.getCenter();
google.maps.event.trigger(map, 'resize');
map.setCenter(currentCenter);
map.setZoom(15);
});
after
map = new google.maps.Map(document.getElementById('map-canvas2'),mapOptions);
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.
<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>
<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.
try this !
<div class="modal fade" id="map_modal" 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-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body"><div id="map_canvas" style="width:530px; height:300px"></div></div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
var map = marker = new Array();
geocoder = NULL;
function addPoint(location, id, mapId) {
if (!marker[id]) {
marker[id] = new google.maps.Marker({
position: location,
map: map[mapId],
draggable: true
});
}
}
function placeMarker(location, editStr, id) {
if (!marker[id]) {
marker[id] = new google.maps.Marker({
position: location,
map: map[id],
draggable: false
});
}
marker[id].setPosition(location);
map[id].setCenter(location);
$("#longitud").val(location.lat());
$("#latitud").val(location.lng());
}
function initializeMap(id, div_id, lat, lng, editStr) {
if (!geocoder)
geocoder = new google.maps.Geocoder();
if (!map[id]) {
var coordinates = new google.maps.LatLng(lat, lng);
var myOptions = {zoom: 8, center: coordinates, mapTypeId: google.maps.MapTypeId.ROADMAP}
map[id] = new google.maps.Map(document.getElementById(div_id), myOptions);
google.maps.event.addListener(map[id], 'click', function(event) {
placeMarker(event.latLng, editStr, id);
});
placeMarker(coordinates, editStr, id);
}
}
$('#map_modal').on('shown.bs.modal', function(e) {
initializeMap("#newMaps", "map_canvas", 10.444598, -66.9287, "");
})
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();
});