Javascript Google 地图 - 未捕获的 InvalidValueError:初始化不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38369151/
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
Google Maps - Uncaught InvalidValueError: initialise is not a function
提问by Lee
When I load the page that my Google Maps is displayed, I always see the following error in the console:
当我加载显示我的 Google 地图的页面时,我总是在控制台中看到以下错误:
Uncaught InvalidValueError: initialise is not a function js?sensor=false&callback=initialise:94
未捕获的 InvalidValueError:初始化不是函数js?sensor=false&callback=initialise:94
When hovering over the filename, this is showing as originating from https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialise
将鼠标悬停在文件名上时,显示为源自https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialise
The Google Maps window and map displays absolutely fine though and has full functionality. Strangely, I couldn't find any hits on Google regarding this, they all seem to be about setLong and setLat.
谷歌地图窗口和地图显示非常好,并且具有完整的功能。奇怪的是,我在谷歌上找不到任何关于此的点击,它们似乎都是关于 setLong 和 setLat。
If I change the order of loading between the API call, and the JS file, the error message changes between initialise
and google
. But in both cases, the map still continues to load fine.
如果我更改 API 调用和 JS 文件之间的加载顺序,错误消息会在initialise
和之间更改google
。但是在这两种情况下,地图仍然可以正常加载。
Why is the error occuring, and how do I resolve it properly? Here's my google-map.js file:
为什么会发生错误,我该如何正确解决?这是我的 google-map.js 文件:
function initialise() {
var myLatlng = new google.maps.LatLng(51.126500, 0.257595); // Add the coordinates
var mapOptions = {
zoom: 15, // The initial zoom level when your map loads (0-20)
disableDefaultUI: true, // Disable the map UI
center: myLatlng, // Centre the Map to our coordinates variable
mapTypeId: google.maps.MapTypeId.ROADMAP, // Set the type of Map
scrollwheel: false, // Disable Mouse Scroll zooming (Essential for responsive sites!)
// All of the below are set to true by default, so simply remove if set to true:
panControl:false, // Set to false to disable
mapTypeControl:false, // Disable Map/Satellite switch
scaleControl:false, // Set to false to hide scale
streetViewControl:false, // Set to disable to hide street view
overviewMapControl:false, // Set to false to remove overview control
rotateControl:false // Set to false to disable rotate control
}
var map = new google.maps.Map(document.getElementById('map'), mapOptions); // Render our map within the empty div
var image = new google.maps.MarkerImage('/wp-content/themes/bellavou/img/marker2.png', null, null, null, new google.maps.Size(70,70)); // Create a variable for our marker image.
var marker = new google.maps.Marker({ // Set the marker
position: new google.maps.LatLng(51.125887, 0.258075), // Position marker to coordinates
icon:image, //use our image as the marker
map: map, // assign the market to our map variable
title: 'Bella Vou at The Pantiles' // Marker ALT Text
});
}
google.maps.event.addDomListener(window, 'load', initialise); // Execute our 'initialise' function once the page has loaded.
回答by MrUpsidown
First, you define a callback on the API call:
首先,您在 API 调用上定义回调:
https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialise
https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialise
then you add a listener
然后你添加一个监听器
google.maps.event.addDomListener(window, 'load', initialise);
google.maps.event.addDomListener(window, 'load', initialise);
that basically does the same.
这基本上是一样的。
You should removethe callback=initialise
from the API call orthe above mentioned addDomListener
line from your JS file and it should work.
您应该删除了callback=initialise
由API或上述addDomListener
从您的JS文件行,它应该工作。