简体   繁体   English

Javascript一直在说Google未定义

[英]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. 我正在尝试使用https://developers.google.com/maps/documentation/javascript/examples/layer-kml中的示例代码(进行了一些修改,例如lat / long),但是每次我尝试运行它时,它说Google未定义。 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: 因为是这样,所以您需要先获取Goggle的API:

<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. 在开发人员指南中查找此处如何获取API密钥

If you look at the HTML+JavaScript notice they included the API at the top: 如果您查看HTML + JavaScript,请注意,它们在顶部包含API:

 <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 在您发送的链接中,有一个名为“入门”的部分: 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). 在其中,您将发现要使用API​​功能的HTML页面的基本结构需要声明包含API代码的Javascript(即,该代码还将提供您需要的“ google”对象) 。

https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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