samedi 23 juillet 2016

Initialize Maps API in module pattern

I am trying to initialize Google Maps API in module pattern but all I get are the errors:

Uncaught ReferenceError: google is not defined

and

Uncaught InvalidValueError: initMap is not a function

gmap.js:

var map,
    mapsAPI = (function() {
        function initMap() {
            map = new google.maps.Map(document.getElementById('mapAddress'), {
                center: {lat: -34.397, lng: 150.644},
                zoom: 8,
                scrollwheel: false
            });
        }

        function init() {
            mapsAPI.initMap();
        }

        window.addEventListener('load', init());
    }());

On the html I am loading in this order (note that I removed my API Key from this example):

<script src="https://maps.googleapis.com/maps/api/js?key=MY_KEY=initMap"
        async defer></script>

<script src="scripts/gmap.js"></script>

If I put the initMap() on Global scope the map works but I am trying to avoid using Global variables.

Am I missing something here?

Aucun commentaire:

Enregistrer un commentaire