简体   繁体   中英

Uncaught Reference Error: google is not defined

I saw a dozen of questions with the same title & the problem. After going through all those problems & making changes in the code, I wasn't able to resolve this issue.

My Google map works fine. But in the console, this error is shown. Can anyone please help me figure out this problem.

I specified JavaScript code inside the head tag

 <script>
    function loadScript()
    {
    var myKey = "__API-Key__";
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.googleapis.com/maps/api/js?key="+myKey+"&sensor=false&callback=initialize";
    document.body.appendChild(script);
    }
    window.onload = loadScript;
    </script>

    <script>

        function initialize()
        {
            var laa=-34.397;
            var lonn= 150.644;
            var mapOptions =
            {
                zoom: 7,
                center: new google.maps.LatLng(laa, lonn),
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                maxZoom: 8,
                minZoom:2
            };

            var map = new google.maps.Map(document.getElementById('location-canvas'),
                mapOptions);

            var marker = new google.maps.Marker({
                map: map,
                draggable: false,
                position: new google.maps.LatLng(laa, lonn)
            });

            function bind(eventName)
            {
                google.maps.event.addListener(map, eventName, function ()
                {
                    common();

                });
            }

            bind('zoom_changed');
            bind('center_changed');
            bind('tilesloaded');
            bind('idle');

            function common()
            {
                var bounds = map.getBounds();
                var southWest = bounds.getSouthWest();
                var northEast = bounds.getNorthEast();
                var getcentre=bounds.getCenter();
                var ne = map.getBounds().getNorthEast();
                var sw = map.getBounds().getSouthWest();
                var zoom=map.getZoom();
                var centre_lat=getcentre.lat();
                var centre_long=getcentre.lng();
                var myLatlng=new google.maps.LatLng(centre_lat,centre_long);
                var mapProp =
                {
                    center: new google.maps.LatLng(centre_lat,centre_long),
                    zoom:zoom,
                    maxZoom: 8,
                    minZoom:2,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

            }
        }

        google.maps.event.addDomListener(window, 'resize', initialize);
        google.maps.event.addDomListener(window, 'load', initialize);

    </script>

Inside Body tag:

            <div id='location-canvas' style='width:100%;height:500px;'>

            </div>

You Don't need to include bellow two lines because you already define a callback function with http://maps.googleapis.com/maps/api/js?key="+myKey+"&sensor=false&callback=initialize

So either use

<script src="http://maps.googleapis.com/maps/api/js?key="+__API-Key__+"&sensor=false" type="text/javascript"></script>

Without function call, put it directly on document

or Remove

   google.maps.event.addDomListener(window, 'resize', initialize);
   google.maps.event.addDomListener(window, 'load', initialize);

eg:

<script src="http://maps.googleapis.com/maps/api/js?key="+__API-Key__+"&sensor=false"></script>

<script>

    function initialize()
    {
        var laa=-34.397;
        var lonn= 150.644;
        var mapOptions =
        {
            zoom: 7,
            center: new google.maps.LatLng(laa, lonn),
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            maxZoom: 8,
            minZoom:2
        };

        var map = new google.maps.Map(document.getElementById('location-canvas'),
            mapOptions);

        var marker = new google.maps.Marker({
            map: map,
            draggable: false,
            position: new google.maps.LatLng(laa, lonn)
        });

        function bind(eventName)
        {
            google.maps.event.addListener(map, eventName, function ()
            {
                common();

            });
        }

        bind('zoom_changed');
        bind('center_changed');
        bind('tilesloaded');
        bind('idle');

        function common()
        {
            var bounds = map.getBounds();
            var southWest = bounds.getSouthWest();
            var northEast = bounds.getNorthEast();
            var getcentre=bounds.getCenter();
            var ne = map.getBounds().getNorthEast();
            var sw = map.getBounds().getSouthWest();
            var zoom=map.getZoom();
            var centre_lat=getcentre.lat();
            var centre_long=getcentre.lng();
            var myLatlng=new google.maps.LatLng(centre_lat,centre_long);
            var mapProp =
            {
                center: new google.maps.LatLng(centre_lat,centre_long),
                zoom:zoom,
                maxZoom: 8,
                minZoom:2,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

        }
    }

    google.maps.event.addDomListener(window, 'resize', initialize);
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

        <div id='location-canvas' style='width:100%;height:500px;'>

        </div>

This should works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM