![](/img/trans.png)
[英]Cannot call method 'apply' of undefined on setting directions for jquery google maps plugin
[英]Google Maps Issue Cannot call method 'apply' of undefined?
我已經在谷歌搜索了一個解決方案,但這似乎是一個新的? 我正在嘗試在網站上實施google maps API但是我一直收到以下錯誤:
未捕獲的TypeError:無法調用未定義的方法'apply'
我的JS如下:
var map;
function initialize(location) {
var mapDiv = document.getElementById('map-canvas');
var latLng;
if (location == 0) {
latLng = new google.maps.LatLng(52.066356, 1.102388);
}
else if (location == 1) {
latLng = new google.maps.LatLng(52.672492, 1.232196);
}
else if (location == 2) {
latLng = new google.maps.LatLng(52.207607, 0.123017);
}
map = new google.maps.Map(mapDiv, {
center: latLng,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
google.maps.event.addDomListener(map, 'tilesloaded', addMarker(location));
}
function addMarker(location) {
var latLng;
if (location == 0) {
latLng = new google.maps.LatLng(52.066703, 1.113573);
}
else if (location == 1) {
latLng = new google.maps.LatLng(52.672492, 1.232196);
}
else if (location == 2) {
latLng = new google.maps.LatLng(52.207607, 0.123017);
}
var marker = new google.maps.Marker({
position: latLng,
map: map
});
}
$(document).ready(function () {
initialize(0);
});
在這一行
google.maps.event.addDomListener(map, 'tilesloaded', addMarker(location));
您正在調用函數addMarker而不是將其作為函數引用傳遞。 因此,在定義map之前,addMarker正在執行。 嘗試將該行更改為:
google.maps.event.addDomListener(map, 'tilesloaded', function() {addMarker(location)});
問題是你正在調用你應該提供回調的函數addMarker
。 將您的通話更改為以下內容
google.maps.event.addDomListener(map, 'tilesloaded', function() { addMarker(location) });
現在你直接調用addMarker
,它不返回任何值。 這意味着您有效地將undefined
傳遞給API,並且谷歌庫正在嘗試調用您的回調
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.