我只想在地圖上移動標記,並聲明變量map
,爲全局,但我無法使用它功能moveMarker()
。我是JavaScript新手,所以這個問題可能很簡單,但我仍然無法解決它 - 任何人都可以幫我解決它嗎?Google Maps API v3無法調用未定義的方法「panTo」
var map;
var marker;
function initialize() {
var myLatLng = new google.maps.LatLng(50, 50),
myOptions = {
zoom: 4,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
marker = new google.maps.Marker({
position: myLatLng,
map: map,
draggable: true
});
marker.setMap(map);
setTimeout("moveMarker(1)", 2000);
}
function moveMarker(i) {
//delayed so you can see it move
if (i > 10) {
return;
}
var myLatLng = new google.maps.LatLng(50 + 0.1 * i, 50 + 0.1 * i);
marker.setPosition(myLatLng);
map.panTo(myLatLng);
var latlng = marker.getPosition();
newlatlng = latlng.toString();
marker.setTitle(newlatlng);
setTimeout("moveMarker(" + (i + 1) + ")", 1500);
}
window.onload = function() {
// Setup the dnd listeners.
initialize();
};
其實myLatLng並不需要是全球性的,它只是函數的範圍內使用。除非您需要,否則無需將變量定義爲全局變量。 – albattran