我不明白爲什麼setTimeout不能按我的預期工作。setTimeout()和在谷歌地圖上放置標記
我想在不同的時間放下每個標記,而不是全部在同一時間。
粘貼並運行此代碼爲http://jsfiddle.net,然後才能看到有趣的行爲刪除註釋:
//setTimeout(function() {
addMarker(m);
//}, i * 500);
。
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var berlin = new google.maps.LatLng(52.520816, 13.410186);
var neighborhoods = [
new google.maps.LatLng(52.511467, 13.447179),
new google.maps.LatLng(52.549061, 13.422975),
new google.maps.LatLng(52.497622, 13.396110),
new google.maps.LatLng(52.517683, 13.394393)
];
var map;
function initialize() {
var mapOptions = {
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: berlin
};
map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);
}
function drop() {
var j = 0;
for (var i = 0; i < neighborhoods.length; i++) {
var m = neighborhoods[i];
//setTimeout(function() {
addMarker(m);
//}, i * 500);
}
}
function addMarker(m) {
new google.maps.Marker({
position: m,
map: map,
draggable: false,
animation: google.maps.Animation.DROP
});
}
</script>
<body onload="initialize()">
<div id="map_canvas" style="width: 500px; height: 400px;">map div</div>
<button id="drop" onclick="drop()">Drop Markers</button>
</body>
爲什麼不把代碼粘貼到jsfiddle中並將鏈接發佈到它? – 2012-02-15 14:00:04
jsfiddle正在遷移服務器,目前任何人都無法保存代碼 – lito 2012-02-15 14:05:55