[英]Uncaught ReferenceError: google is not defined
我想使用地理定位和方向功能,但google is not defined
錯誤。 代碼如下:
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);
}
似乎loadScript不起作用!
var mapOptions = {
zoom : 13,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
錯誤從這里跳出來。 誰知道如何解決這個問題? 我需要使用密鑰來獲取地理定位服務,所以我不能使用簡單的
<script src="https://maps.googleapis.com/maps/api/js?sensor=true"></script>
如果您在控制台中遇到錯誤,那么這里是我應用的簡單解決方案。
包含給定順序的腳本文件..
<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>
你必須首先包括'maps.googleapis.com/maps/api/js?sensor=false'然后去jquery庫並從下面刪除它(它會工作。)我希望它肯定會有效。
我用這個代碼自己嘗試了 - 它對我來說很好
用鑰匙動態
<!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>
靜態無鍵
...
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false">
</script>
</head>
<body onload="initialize()">
...
當我在網上沖浪時,我翻過一個重要的音符!
Google Maps JavaScript API v3
Google Maps JavaScript API v3不需要API密鑰即可正常運行。 不過,我們強烈建議您使用API控制台密鑰加載Maps API,以便監控應用程序的Maps API使用情況。 了解如何使用API控制台密鑰。
所以,顯然你不再需要開發人員密鑰了! 我嘗試了它 - 靜態無鍵和動態鍵 - 兩者都有效。
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>
這意味着如果你在https上
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.