Is there a way to detect when the user clicks the default fullscreen mode button?
These are the map options I'm using:
var mapOptions = {
center: {
lat: 40.907192,
lng: 20.036871
},
zoom: 2,
fullscreenControl: true
};
bounds_changed event fires repeatedly when the map is moving.
Overlays are objects on the map that are tied to latitude/longitude coordinates, so they move when you drag or zoom the map. For information on predefined overlay types, see Drawing on the map.
DaveBurns answer worked with some modifications because I don't use jQuery. Also I needed to use clientHeight and clientWidth
window.google.maps.event.addListener(
map,
'bounds_changed',
onBoundsChanged,
);
function onBoundsChanged() {
if (
map.getDiv().firstChild.clientHeight === window.innerHeight &&
map.getDiv().firstChild.clientWidth === window.innerWidth
) {
console.log('FULL SCREEN');
} else {
console.log('NOT FULL SCREEN');
}
}
I'm not sure if you want to detect the actual click event or just the state change between full screen and not. I needed to do the latter. The two things you need to know are that a) when the map size changes, the map will fire the bounds_changed
event and b) within your event handler for that, you need to compare the map div's size with the size of the entire screen. Do this like so:
google.maps.event.addListener( map, 'bounds_changed', onBoundsChanged );
function onBoundsChanged() {
if ( $(map.getDiv()).children().eq(0).height() == window.innerHeight &&
$(map.getDiv()).children().eq(0).width() == window.innerWidth ) {
console.log( 'FULL SCREEN' );
}
else {
console.log ('NOT FULL SCREEN');
}
}
Note that getDiv() returns your own div that you passed to the Map() constructor. That div doesn't get resized for full screen - its child does. So that part where I'm getting the child of the map's div is correct but a little unwieldy. You could also rewrite that more briefly like this and it will work but this could break in the future if Google changes the map div's class name:
if ( $( '.gm-style' ).height() == window.innerHeight &&
$( '.gm-style' ).width() == window.innerWidth ) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With