[英]simulate route on Google maps
I have some problems when I want to simulate a route paths on Google map. 我想在Google地图上模拟路线时遇到一些问题。
closure
issue. closure
问题。 setTimeout
time interval dynamically. setTimeout
时间间隔。 I have a list of location data, there are different useful properties in lacation object for me to render on the Google maps, like Lat,Lng and Time etc... 我有一个位置数据列表,在Laposition对象中有一些有用的属性供我在Google地图上呈现,例如Lat,Lng和Time等。
I want to create a Google maps Marker every 500ms(*change interval dynamically) to simulate the route paths on the maps, but every time I got the same location data. 我想每500毫秒(*动态更改间隔)创建一个Google Maps Marker,以模拟地图上的路线,但是每当我得到相同的位置数据时。
here are the JavaScript code: 这是JavaScript代码:
function playback(data) {
data = [{ Time: 2010, Lat: 1.36046, Lng: 103.897018 }, { Time: 2011, Lat: 1.352566, Lng: 103.855768 }, { Time: 2012, Lat: 1.349477, Lng: 103.802553}];
for (i = 0; i < data.length; i++) {
setTimeout(function () { printlocation(data[i]); }, i * 500);
}
}
function printlocation(cur) {
alert(cur.Time);
}
When the code runs, it prints out 2012 for 3 times. 代码运行时,它将打印3次2012。
after research, it probably because of the JavaScript closure
, but I don't really understand how it works. 经过研究,这可能是由于JavaScript
closure
,但我不太了解它是如何工作的。 I want to find a pattern to solve my 2 problems. 我想找到一种模式来解决我的2个问题。 thanks in advanced.
提前致谢。
Untested: 未经测试:
// Global variable
var dataArray = [{ Time: 2010, Lat: 1.36046, Lng: 103.897018 }, { Time: 2011, Lat: 1.352566, Lng: 103.855768 }, { Time: 2012, Lat: 1.349477, Lng: 103.802553}];
function playback() {
for (i = 0; i < dataArray.length; i++) {
var data = getData(i);
setTimeout(function () { printlocation(data); }, i * 500);
}
}
function getData(ix) {
return dataArray[ix];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.