Recenter a Google map after container changed width

Calling resize by it self will not acheive what you need.

What you need to do is first (before a resize occurs) get the current center of the map

var currCenter = map.getCenter();

Then you need to do something like the following, after your div is resized.

google.maps.event.trigger(map, 'resize');
map.setCenter(currCenter);

Should do the trick

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)