125

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);
});
4
  • 1
    good job! yes you have to continuously output the current center value to global scope so it can be picked up by the dom listener
    – ericjam
    Commented Jan 4, 2013 at 23:12
  • If you are going top do it this way, then center does not need to be global. Providing center (and calculateCenter) are in the same scope as map, then everything should work. Of course, if map is global then you'll need to fix that too :) Commented Apr 16, 2014 at 6:43
  • Maybe it's obvious to most, but this code comes AFTER the map is loaded, so it should be at least on windows.onload Commented Aug 18, 2015 at 15:32
  • thanks, this should be the default google maps beaviours
    – Yukulélé
    Commented Aug 31, 2017 at 13:32

5 Answers 5

142

You need to have an event listener for when the window resizes. This worked for me (put it in your initialize function):

google.maps.event.addDomListener(window, 'resize', function() {
    map.setCenter(center);
});
11
  • This works perfect, thanks! But if someone has moved the map, how do I get the new center of the map? I found the getCenter() function, but can't get it to work correctly. map.setCenter(map.getCenter()); doesn't work. Commented Jan 10, 2012 at 10:19
  • You want another event listener, I think for "center_changed" on the map object, that updates a global variable with the new coordinates, which you can then use in your setCenter.
    – duncan
    Commented Jan 10, 2012 at 11:15
  • 1
    I tried that as well: var center; google.maps.event.addListener(map, 'center_changed', function() { center = map.getCenter(); }); google.maps.event.addDomListener(window, 'resize', function() { map.setCenter(center); }); Doesn't work. Any help would be appreciated. Commented Jan 10, 2012 at 11:27
  • How about "bounds_changed" instead?
    – duncan
    Commented Jan 10, 2012 at 12:03
  • 49
    This is the easiest way: hsmoore.com/blog/…
    – AO_
    Commented Mar 8, 2013 at 0:06
83

Detect the browser resize event, resizes the Google Map while preserving the center point:

var map = ...; // your map initialisation code

google.maps.event.addDomListener(window, "resize", function() {
    var center = map.getCenter();
    google.maps.event.trigger(map, "resize");
    map.setCenter(center); 
});

You can use the same code in other event handlers when resizing the google map via other means (e.g. with a jQuery-UI 'resizable' control).

Source: http://ataiva.com/keep-google-map-v3-centered-when-browser-is-resized/ credit to @smftre for the link.

Note: This code was linked in @smftre's comment on the accepted answer. I think it's worth adding it as its own answer since it is really superior to the accepted answer. It eliminates the need of a global variable to track the center point and an event handler to update that variable.

6
  • Agree with @William, this method results in a more accurate center-position when the map is resized.
    – tenthfloor
    Commented Feb 2, 2014 at 17:08
  • accepted answer wasn't working correctly, this one did the trick
    – Tom
    Commented Mar 17, 2014 at 11:52
  • This is the best and working solution. Works on every resize windows event. Other solutions didnt work properly. Commented Sep 11, 2014 at 21:38
  • 1
    This really should have been posted as community wiki. Commented Jan 27, 2015 at 12:41
  • It works, but I'd like to understand it more. What precisely does google.maps.event.trigger(map, "resize"); ?
    – meduz'
    Commented Jun 26, 2015 at 13:22
4

Some good examples on w3schools.com. I used the following and it works great.

google.maps.event.addDomListener(window, 'resize', function() {
  map.panTo(myLatlng);
});

http://www.w3schools.com/googleapi/google_maps_events.asp

0
0

html: Create a div with an id of your choice

<div id="map"></div>

js: Create a map and attach it to the div you just created

var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 16,
    center: new google.maps.LatLng(-33.890542, 151.274856)
});

Center when the window resizes

var center = map.getCenter();

google.maps.event.addDomListener(window, 'resize', function() {
    map.setCenter(center);
});
0

Updated of the above solution to es6.

let center;
const addMapListener = google.maps.event.addDomListener;

addMapListener(map, 'idle', () => center = map.getCenter());
addMapListener(window, 'resize', () => map.setCenter(center));
1
  • You're using the addDomListener for both the window, which is a DOM element, and the map, which isn't. The map object should use the google.maps.event.addListener or its own map.addListener event handler.
    – duncan
    Commented May 1, 2017 at 22:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.