objectivehtml / Google-Maps-for-Craft

The complete geolocation toolkit for Craft CMS.
Other
94 stars 19 forks source link

Keep map centred when browser window size changes #40

Closed katrinkerber closed 8 years ago

katrinkerber commented 9 years ago

What's the best way to make use of this solution, which keeps the map centred when the browser window size changes?

objectivehtml commented 8 years ago

I know this is super old, but I ended up having some health issues and didn't work most of last year. I'm back now though and figured I would post the solution to this for future reference.

{% set options = {
    id: 'map', 
    width: '100%', 
    height: '100%',
    center: 'The Florida Keys'
} %}

{{ craft.googleMaps.map(options) }}

<script type="text/javascript">
    var mapCenter;

    window.onload = function() {
        mapCenter = map.getCenter();
    };

    window.onresize = function(e) {
        map.setCenter(mapCenter.lat(), mapCenter.lng());
    };

</script>