javascript 本地主机的 Google Maps v3 api 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19784661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:46:56  来源:igfitidea点击:

Google Maps v3 api for localhost not working

javascriptgoogle-mapsgoogle-maps-api-3

提问by Neville

I wanted to try out a sample demo of implementing Gmaps v3 and tried out this example from Google's documentation, but there is no output , the page just loads for a few seconds and then blank, no output.

我想尝试一个实现 Gmaps v3 的示例演示,并从 Google 的文档中尝试了这个示例,但是没有输出,页面只是加载了几秒钟然后空白,没有输出。

<!DOCTYPE html>
<html lang = "en">
<head>
    <style type="text/css">
        html{height: 100%}
        body{height: 100%; margin: 0; padding: 0}
        #map-canvas{height: 100%}
    </style>
    <title>GMaps Demo</title>
    <script src = "https://maps.googleapis.com/maps/api/js?
                   key=${API_KEY}&sensor=false">
    </script>
    <script>
        function initialize(){
            var mapOptions = {
                center: new google.maps.LatLng(-34.397, 150.644),
                zoom: 8,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = google.maps.Map(
                      document.getElementById("map-canvas"),
                      mapOptions);
        }

        google.maps.event.addDomListener(window, 'load', initialize);
        </script>
</head>
<body>
    <div id = "map-canvas">
    </div>
</body>
</html>

回答by Joachim Rohde

First of all: Google Maps 3 does not need an api key anymore, so you are fine with

首先:Google Maps 3 不再需要 api 密钥,因此您可以使用

https://maps.googleapis.com/maps/api/js?sensor=false
https://maps.googleapis.com/maps/api/js?sensor=false

as an URL.

作为网址。

(As Jeff Hoye pointed out, as of June 22, 2016, an API Key is required again.)

(正如 Jeff Hoye 指出的,截至 2016 年 6 月 22 日,再次需要 API 密钥。)

And then you forgot the "new" in this line:

然后你忘记了这一行中的“新”:

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

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

With those changes your map will be displayed.

通过这些更改,您的地图将显示出来。

回答by Ravi Ji

Without an API Key now Google Maps does not work... reference: https://console.cloud.google.com/apis

如果现在没有 API 密钥,Google 地图将无法工作...参考:https: //console.cloud.google.com/apis

回答by Ebrahim

it's very easy! in the new API code, you should enter your referrers. So for your localhost test:

这很容易!在新的 API 代码中,您应该输入您的推荐人。所以对于你的本地主机测试:

LEAVE IT EMPTY

让它空着

here is also the google api explanation:

这里也是谷歌api解释:

 Accept requests from these HTTP referrers (web sites) (Optional)
 Use asterisks for wildcards. 

If you leave this blank, requests will be accepted from any referrer.
Be sure to add referrers before using this key in production.

如果您将此留空,则将接受来自任何推荐人的请求。
在生产中使用此密钥之前,请务必添加引用者。