[英]google maps animated marker moving
So im trying to move marker smoothly, but id doesnt work. 所以我试图顺利移动标记,但id不起作用。 Marker is moving, but not smoothly, the same result i can have if i will write
Marker正在移动,但不是很顺利,如果我要写的话,我可以拥有相同的结果
marker[n].setPosition(moveto);
i have displayed all variables in console, and its fine, all increases by right way, but it seems like 我已经在控制台中显示了所有变量,并且很好,所有变量都以正确的方式增加,但似乎是
marker[n].setPosition(latlng);
is called only at the last iteration of cycle. 仅在循环的最后一次迭代时调用。
here is my code: 这是我的代码:
function animatedMove(n, current, moveto){
var lat = current.lat();
var lng = current.lng();
var deltalat = (moveto.lat() - current.lat())/100;
var deltalng = (moveto.lng() - current.lng())/100;
for(var i=0;i<100;i++){
lat += deltalat;
lng += deltalng;
latlng = new google.maps.LatLng(lat, lng);
setTimeout(
function(){
marker[n].setPosition(latlng);
},10
);
}
}
What your code is doing is 你的代码正在做什么
for(var i=0;i<100;i++){
// compute new position
// run function "f" that updates position of marker after a delay of 10
What happens is that by the time the function "f" (( (function(){marker[n].setPosition(latlng);}
) runs after the delay, the cycle has already finished and it has reached the final position for the marker. 发生的事情是,当函数“f”((
(function(){marker[n].setPosition(latlng);}
)在延迟之后运行时,循环已经完成并且它已到达最终位置标记。
Following https://stackoverflow.com/a/24293516/2314737 here's one possible solution 以下https://stackoverflow.com/a/24293516/2314737这是一个可能的解决方案
function animatedMove(n, current, moveto) {
var lat = current.lat();
var lng = current.lng();
var deltalat = (moveto.lat() - current.lat()) / 100;
var deltalng = (moveto.lng() - current.lng()) / 100;
for (var i = 0; i < 100; i++) {
(function(ind) {
setTimeout(
function() {
var lat = marker.position.lat();
var lng = marker.position.lng();
lat += deltalat;
lng += deltalng;
latlng = new google.maps.LatLng(lat, lng);
marker.setPosition(latlng);
}, 10 * ind
);
})(i)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.