Google Maps API v3 - how to detect when map changes to full screen mode?

后端 未结 4 920
盖世英雄少女心
盖世英雄少女心 2020-12-07 01:41

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 = {
                   


        
相关标签:
4条回答
  • 2020-12-07 02:04

    You can use HTML5 Fullscreen API which has the fullscreenchange event:

    "When fullscreen mode is successfully engaged, the document which contains the element receives a fullscreenchange event. When fullscreen mode is exited, the document again receives a fullscreenchange event".

    Please note that

    "For the moment not all browsers are using the unprefixed version of the API".

    So, for instance, in Mozilla Firefox the event handler would look like this

    document.onmozfullscreenchange = function(event) { 
        console.log("Full screen change")
    }
    
    0 讨论(0)
  • 2020-12-07 02:09

    This solution it is working for me:

    /** Full Screen event */
    
    $(document).bind('webkitfullscreenchange mozfullscreenchange fullscreenchange', function() {
        var isFullScreen = document.fullScreen ||
            document.mozFullScreen ||
            document.webkitIsFullScreen;
        if (isFullScreen) {
            console.log('fullScreen!');
        } else {
            console.log('NO fullScreen!');
        }
    });
    
    0 讨论(0)
  • 2020-12-07 02:26

    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 ) {
    
    0 讨论(0)
  • 2020-12-07 02:26

    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');
      }
    }
    
    0 讨论(0)
提交回复
热议问题