简体   繁体   中英

Google Maps v3, error message “Google is undefined”

I am getting errors on my site just trying to implement the basic "Hello world" application:

<div id="map"></div> 

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>

$(document).ready(function () {
    map = new google.maps.Map(document.getElementById("map"), myOptions);
});

The error I get is:

'Uncaught ReferenceError: google is not defined'

How do I fix this problem?

Your source is wrong, I also suggest specifing the exact version you want to load:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&sensor=true"></script>

That should help.

Sometimes this happens in the browser Google Chrome if your main site is in a https platform and the map is in a http. For showing the map you have a shield in URL box, just click the shield and it should show it.

Lots of things from your code:

  1. You have given document.ready but there is no jQuery link

  2. Your document.ready has to be inside <script> tags </script>

  3. You should also have given the height and width to the div in which you are holding the map, as such it is just an empty tag.

  4. You have defined myOptions for Map, and there is nothing to interpret for map as what are the options to be taken into consideration while loading the map**

Here is a Complete example:

<html>
<body>

<div id="map" style="width:500px;height:500px;"></div> 
<script type="text/javascript" src="Please put your jquery here"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
var myLatlng = new google.maps.LatLng(40.65, -73.95);
  var myOptions = {
    zoom: 13,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
$("document").ready(function () {

    map = new google.maps.Map(document.getElementById("map"), myOptions);
});
</script>
</body>
</html>

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