我只想在地图上移动标记,并将变量声明为全局变量map
,marker
但我无法在函数中使用它moveMarker()
。我是 JavaScript 新手,所以这个问题可能很简单,但我仍然无法解决 - 任何人都可以帮我解决它吗?
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();
};