Calling Google maps infowindow.open causes website to scroll down to the infowindow

Viewed 505

I am pretty sure this was not the default behaviour for Google Maps API for quite some time, but it must have changed in the meantime.

I modified Fiddle provided by google: https://developers.google.com/maps/documentation/javascript/infowindows

added 1000px "content" div on on top:

<div style="height: 1000px">CONTENT</div>

and added "auto" opening of infowindow to the script

google.maps.event.addListenerOnce(map, 'tilesloaded', function () {
  infowindow.open(map, marker);
});

JS Fiddle:

https://jsfiddle.net/ym3c2e8u/

Problem:

If you comment out opening infowindow line, notice that the page will load and show you the top "CONTENT".
If the infowindow is being opened, the browser will automatically scroll down to centre on this element.

This is really frustrating and I would like to prevent this behaviour.

1 Answers

The default behavior of InfoWindow.open() is to have the browser focus on the element. This can be changed by using InfoWindowOpenOptions.

infowindow.open({anchor: marker, shouldFocus: false});

instead of:

infowindow.open(map, marker);

Related