I am using the Google Maps API v2. I add markers to the map and then scale to fit these markers. This works great if the map is visible, I do it. But if not - for example, if I have a tab, and the map tab will not be selected when the page loads - then when I show the map, the zoom level and center will be wrong.
Here is a simple test case (uses jQuery):
<script type="text/javascript">
var scale = Math.random() * 20;
$(document).ready(function() {
var $container = $('#container');
var map = new GMap2($('#map')[0]);
$container.show();
var markerBounds = new GLatLngBounds();
for (var i = 0; i < 10; i++) {
var randomPoint = new GLatLng(38.935394 + (Math.random() - 0.5) * scale, -77.061382 + (Math.random() - 0.5) * scale);
map.addOverlay(new GMarker(randomPoint));
markerBounds.extend(randomPoint);
}
map.setCenter(markerBounds.getCenter(), map.getBoundsZoomLevel(markerBounds));
});
</script>
<div id="container">
<div id="map" style="margin: 100px; width: 450px; height: 300px;"></div>
</div>
This works fine, but if you uncomment $container.hide(), everyone deleted it.
Is there a way to get the Google Maps API to work fine on a div that is not displaying?