简体   繁体   English

未捕获的ReferenceError:未定义谷歌

[英]Uncaught ReferenceError: google is not defined

I want to use geolocation and direction function, but there is google is not defined error. 我想使用地理定位和方向功能,但google is not defined错误。 the code is as below: 代码如下:

function loadScript() {
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = "https://maps.googleapis.com/maps/api/js?key=mykey&sensor=true" + "&callback=initialize";
            document.body.appendChild(script);
        }

It seems that the loadScript does not work! 似乎loadScript不起作用!

var mapOptions = {
                zoom : 13,
                mapTypeId : google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

The error jumps out from here. 错误从这里跳出来。 Is anyone who know how to figure it out? 谁知道如何解决这个问题? I need to use key to get the geolocation service, so I cannot use simple 我需要使用密钥来获取地理定位服务,所以我不能使用简单的

<script src="https://maps.googleapis.com/maps/api/js?sensor=true"></script>

If you people are getting Error in console then here is the simple solution that I applied. 如果您在控制台中遇到错误,那么这里是我应用的简单解决方案。

include the script files in given sequence.. 包含给定顺序的脚本文件..

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>

You must first include 'maps.googleapis.com/maps/api/js?sensor=false' first then go for jquery library and remove it from below both (it will work.) I hope it will definitely work. 你必须首先包括'maps.googleapis.com/maps/api/js?sensor=false'然后去jquery库并从下面删除它(它会工作。)我希望它肯定会有效。

I tried it on my own with this code - it worked fine for me 我用这个代码自己尝试了 - 它对我来说很好

Dynamic with key 用钥匙动态

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        html {  height: 100%; }
        body
        {
            height: 100%;
            margin: 0px;
            padding: 0px;
        }
        #map_canvas { height: 100%;}
    </style>
    <script type="text/javascript">
        function initialize() {
            var latlng = new google.maps.LatLng(-34.397, 150.644);
            var myOptions = {
                zoom: 8,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
        }
        var myKey = "ENTER_YOUR_KEY_HERE";
        function loadScript() {
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = "https://maps.googleapis.com/maps/api/js?key=" + myKey + "&sensor=false&callback=initialize";
            document.body.appendChild(script);
        }
    </script>
</head>
<body onload="loadScript()">
    <div id="map_canvas" style="width: 100%; height: 100%">
    </div>
</body>
</html>

Static without key 静态无键

  ...
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false">
</script>
</head>
<body onload="initialize()">
   ...

When surfing through the net I tumbled over an important note! 当我在网上冲浪时,我翻过一个重要的音符!

Google Maps JavaScript API v3 Google Maps JavaScript API v3

The Google Maps JavaScript API v3 does not require an API key to function correctly. Google Maps JavaScript API v3不需要API密钥即可正常运行。 However, we strongly encourage you to load the Maps API using an APIs Console key which allows you to monitor your application's Maps API usage. 不过,我们强烈建议您使用API​​控制台密钥加载Maps API,以便监控应用程序的Maps API使用情况。 Learn how to use an APIs Console key. 了解如何使用API​​控制台密钥。

See Google Maps API 请参阅Google Maps API

So, apparently you no longer need a developer key! 所以,显然你不再需要开发人员密钥了! I tried it with both - static no key and dynamnic with key - both worked. 我尝试了它 - 静态无键动态键 - 两者都有效。

Google is not defined mean that your google map libary is not loaded which mean you are on https or http and you are requesting via http ot https so change it to https or http .... mean if you are on http then Google未定义意味着您的Google地图库未加载,这意味着您使用https或http并且您通过http ot https请求将其更改为https或http ....意味着如果您使用的是http

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

mean if you are on https then 这意味着如果你在https上

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>

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

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