Google Maps API load failure ("'google' is undefined") in IE8

24,031

Solution 1

The problem, apparently, is that IE 8 doesn't grok "application/javascript". I changed it to "text/javascript" in the <script> tag in the <head> section and now my code works. And, of course, if I change it back to "application/javascript", then it stops working.

Solution 2

One guess is that you're page works over https while the request from Google is http. convert the Google request to https and the error will disappear. this worked for me.

See : Possible solution

Solution 3

My google maps v3 site stopped working in IE 9 compatibility view mode, IE 8 and IE7.

Reason: Error in java-script using jQuery only caught when using the IE F12 Developer tools to examine the script. Here is the offending line. The error was missing single quotes from the token, class.

$('<tr>', { 'class': country }).appendTo(tableSelector).append(h1).append(h2);

At first I pursued a false lead, thinking it was the addition of a key= http://maps.googleapis.com/maps/api/js?key=MY_KEY_FROM_API_CONSOLE&sensor=false

The lesson is: use tools, Firebug or IE tools or whatever, to examine your javascript for introduced issues.

Solution 4

Make sure that IE isn't in offline mode. Sounds like the browser isn't connecting to the internet.

Solution 5

IE is downloading then attempting to execute the JS on your local machine, while the other browsers are simply opening it as a text file. You can find the downloaded JS from IE in wherever stuff downloads to by default.

EDIT: In light of updates, see this Fiddle to see a sort-of working fix. http://jsfiddle.net/h6rc3/

Share:
24,031
Trott
Author by

Trott

I like pizza.

Updated on July 09, 2022

Comments

  • Trott
    Trott almost 2 years

    When I load http://maps.google.com/maps/api/js?sensor=false in a script tag, everything works fine for me in Chrome, Safari, Firefox, and IE9.

    However, when I look in IE9 in compatibility mode (or, I'm told, in IE8) the map does not load and "'google' is undefined" is logged in the console.

    Here's the relevant code, with the line that is triggering the error identified with a comment:

    <html>
    <head>
    <title>Test Map</title>
    <script type="application/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    </head>
    <body>
    <div id="map_canvas"></div>
    <script type="text/javascript"> 
    var lat=37.763154;
    var lon=-122.457941;
    var initialZoom=17;
    var mapTypeId = 'Custom Map';
    var mapStyle = [{featureType:"landscape", elementType:"all", stylers:[{hue:"#dae6c3"},{saturation:16},{lightness:-7}]}, 
                    {featureType:"road", elementType:"geometry", stylers:[{hue:"#ffffff"},{saturation:-100},{lightness:100}]}];
    
    //**The error is tripped in this next line, again only in IE9 compatibility mode or IE8*     
    var styledMap = new google.maps.StyledMapType(mapStyle);
    
    var mapType = new google.maps.ImageMapType({
        tileSize: new google.maps.Size(256,256),
        getTileUrl: function(coord,zoom) {
            return "img/tiles/"+zoom+"/"+coord.x+"/"+coord.y+".png";
        }
    });
    var map = new google.maps.Map(document.getElementById("map_canvas"), 
            {center:new google.maps.LatLng(lat,lon),
             mapTypeId:google.maps.MapTypeId.ROADMAP,
             zoom:initialZoom,
             mapTypeControl:false});
    map.overlayMapTypes.insertAt(0, mapType);
    
    map.mapTypes.set(mapTypeId, styledMap);
    map.setMapTypeId(mapTypeId);
    </script>
    </body>
    </html>
    

    So, for some reason, and only in IE9+compatibility-mode and IE8, the script tag specifying http://maps.google.com/maps/api/js?sensor=false isn't loading and/or executing before the subsequent embedded script in the body.

    Are others able to replicate? How do I correct this problem?

  • Trott
    Trott almost 13 years
    Definitely not in offline mode, but that's a good guess. When I turn off compatibility mode in IE9, it works. Turn it on, no map and I get the "'google' is undefined" in the console. It's just compatibility mode (or, according to my user, using IE8) that triggers it.
  • Trott
    Trott almost 13 years
    Oof, I see now. I feel silly. (I don't use IE much, obviously.) OK, so that's not the problem. I'll have to update my question.
  • josh.trow
    josh.trow almost 13 years
    @Trott: No worries, I had to go find IE myself :)