Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FullscreenControl and Maintaining Map Center

Currently, it appears that if I have say a modestly sized Google Maps display port (300px by 300px) with FullscreenControl enabled, and I center that small map view over a specific area, like... France, for instance... And then I hit the full screen button to expand the display to the edges of my screen (1920px by 1080px), France gets tucked wayyyyy up in the top-left corner of my screen.

Basically, the top-left of the original 300px x 300px display moves to the top-left of my screen, and rest of the world map extends from that corner at the original zoom level.

Is there any way to basically just set it up so that the full screen display opens up having the same center point as the original display, and vice versa when the full screen mode gets closed?

Does toggling the full screen button trigger an event or anything that I can hook a setCenter to?

like image 887
Crapachi Avatar asked Oct 26 '16 10:10

Crapachi


2 Answers

I compose a code a bit strange but it works perfectly:

<script>
    var map;
    var center = -1;
    var isFullScreen = false;
    function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
            zoom: 8,
            center: { lat: -34.397, lng: 150.644 }
        });
        center = { lat: map.getCenter().lat(), lng: map.getCenter().lng() };
        google.maps.event.addDomListener(map, 'bounds_changed', onBoundsChanged);
    }
    function onBoundsChanged() {
            var isFullHeight = $(map.getDiv()).children().eq(0).height() == window.innerHeight;
            var isFullWidth = $(map.getDiv()).children().eq(0).width() == window.innerWidth;
            if (isFullHeight && isFullWidth && !isFullScreen) {
                isFullScreen = true;
                map.setCenter(center);
                console.log('FULL');
            } else if (!isFullWidth||!isFullHeight){
                if (isFullScreen) {
                    map.setCenter(center);
                }
                isFullScreen = false;
                console.log('NOT-FULL');
            }
            center = { lat: map.getCenter().lat(), lng: map.getCenter().lng() };            
    }
</script>

I use the bounds_changed event.

If I detect that the map is in full screen I set the map in the center that I note in the precedent event and I set a boolean to true. If the map is not in full screen, I check if the boolean is true, it mean that in the precedent event the map was in full screen, so I recenter the map and next I check if the boolean is false. At the end of the event, I keep the center in a variable fro the next event.

All this are not very clear...

You may consult this post in Stack Overflow.

Notice: This code does not work if you display a single map in all the page of your web app. I do not find a way to remove this bug. Your suggestions are very appreciated, thanks.

Also notice: My code is inspired of the precedent answer. However you can find the same code here. Please notice that it is not a duplicate answer. I add my own part of code in it.

Tell me if you have some questions or comments.

like image 182
SphynxTech Avatar answered Nov 07 '22 09:11

SphynxTech


Try this

/** To detect Map Full Screen event */
google.maps.event.addListener( map, 'bounds_changed', onBoundsChanged ); //Event listener map bound change.
var isMapFullScreen = false;
var defaultLocation = {
    lat: 27.94,
    lng: -82.45
};
function onBoundsChanged() {
    if(!isMapFullScreen){
        var isFullHeight = $(map.getDiv()).children().eq(0).height() == window.innerHeight;
        var isFullWidth= $(map.getDiv()).children().eq(0).width()  == window.innerWidth;
        if (isFullHeight && isFullWidth) {
            isMapFullScreen = true;
            myMarker.setPosition(defaultLocation);
            map.setCenter(defaultLocation);
            console.log('FULL');
        } else {
           isMapFullScreen = false; 
           console.log('NOT-FULL');
        }
    }
}
like image 1
Prasanth Ravi Avatar answered Nov 07 '22 11:11

Prasanth Ravi