I have a Google Maps (V3) in my page at 100% page width with one marker in the middle. When I resize my browser window's width I would like the map to stay centered (responsive). Now the map just stays at the left side of the page and gets smaller.
UPDATE Got it to work exactly as described thanks to duncan. This is the final code:
var center;
function calculateCenter() {
center = map.getCenter();
}
google.maps.event.addDomListener(map, 'idle', function() {
calculateCenter();
});
google.maps.event.addDomListener(window, 'resize', function() {
map.setCenter(center);
});
center
does not need to be global. Providingcenter
(andcalculateCenter
) are in the same scope asmap
, then everything should work. Of course, ifmap
is global then you'll need to fix that too :)