javascript - google maps animated marker moving - Stack Overflow

So im trying to move marker smoothly, but id doesnt work. Marker is moving, but not smoothly, the same

So im trying to move marker smoothly, but id doesnt work. Marker is moving, but not smoothly, the same result i can have if i will write

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
            );
        }
    }

So im trying to move marker smoothly, but id doesnt work. Marker is moving, but not smoothly, the same result i can have if i will write

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
            );
        }
    }
Share Improve this question edited Apr 23, 2022 at 17:07 user2314737 29.5k20 gold badges108 silver badges123 bronze badges asked Sep 20, 2017 at 7:48 Игорь БыстревскийИгорь Быстревский 43710 silver badges26 bronze badges 4
  • Check this sample – codtex Commented Sep 20, 2017 at 7:50
  • thanks, this code works fine, but anyway its interesting what did i do wrong – Игорь Быстревский Commented Sep 20, 2017 at 8:13
  • 2 You are setting 100 timeouts for 10 milliseconds from now. All of them fire at the same time, the marker ends up positioned at the last position... – geocodezip Commented Sep 20, 2017 at 10:11
  • duplicate of JavaScript : For loop with timeout – geocodezip Commented Sep 20, 2017 at 10:12
Add a ment  | 

1 Answer 1

Reset to default 5

What your code is doing is

for(var i=0;i<100;i++){
// pute 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.

Following https://stackoverflow./a/24293516/2314737 here's one possible solution

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)
  }
}

you can look at a demo here

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745608696a4635822.html

相关推荐

  • javascript - google maps animated marker moving - Stack Overflow

    So im trying to move marker smoothly, but id doesnt work. Marker is moving, but not smoothly, the same

    11小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信