简体   繁体   中英

Javascript keeps saying Google is undefined

I'm trying to use the example code from https://developers.google.com/maps/documentation/javascript/examples/layer-kml (with a few modifications, like lat/long) but every time I try to run it, it says google is undefined. As a programmer, I'm pretty green-how do I fix it/prevent it, and what does it mean?

Because it is, you need to get Goggle's API first:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API_KEY"></script>
...
<script>
    /* Code Here */
</script>

Look here for how to get an API key in the developers guide.

If you look at the HTML+JavaScript notice they included the API at the top:

 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>

In the link you sent, there's a section called "Getting Started": https://developers.google.com/maps/documentation/javascript/tutorial

In there you will find out that a basic structure of a HTML page that is going to work with the API functionalities needs a declaration of the Javascript containing the API code (ie, this code will also provide this 'google' object that you need).

https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API

<html>
  <head>
    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?key=API_KEY">
    </script>

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