简体   繁体   English

这里的逻辑有什么问题?

[英]What's wrong with the logic here?

I'm currently working with the Google Maps V3 API, and I'm animating a marker to travel a predetermined route. 我目前正在使用Google Maps V3 API,并且正在为标记设置动画以使其行进预定路线。 The route information is stored in two separate arrays, lat for the latitude coordinates, and lon for the longitude coordinates. 路线信息存储在两个单独的数组中,纬度坐标为lat ,纬度坐标为lon I'm experiencing a logic issue when trying to build buttons to pause and resume the marker's movement. 尝试建立按钮以暂停和恢复标记的移动时遇到逻辑问题。 Here is the code: 这是代码:

  var paused = 0; //paused state
  var interval = 1000; //interval for animation (ms)
  function clickPause() {
      paused = 1;
  }
  function clickPlay() {
      paused = 0;
  }
  function moveToStep(yourmarker,yourroute,c) {
      var LatLon;
      var time;
      var hours;
      var minutes;
      var seconds;
      var finalTime;
      var stopTime;
      if (yourroute.length > c) {
          LatLon = new google.maps.LatLng(lat[c],lon[c]);
          yourmarker.setPosition(LatLon);
          document.getElementById("currlat").innerHTML = lat[c];
          document.getElementById("currlon").innerHTML = lon[c];
          document.getElementById("currtime").innerHTML = c+1;
          hours = 7+Math.floor((c+1)/3600);
          minutes = Math.floor((c+1)/60);
          seconds = (c+1)-minutes*60;
          if(minutes == 60) {
              minutes = 0;
          }
          finalTime = str_pad_left(hours,'0',2)+':'+str_pad_left(minutes,'0',2)+':'+str_pad_left(seconds,'0',2);
          document.getElementById("finaltime").innerHTML = finalTime;
          if(paused == 1) {
              stopTime = c+1;
              window.setInterval(function(){
                  if(paused == 0) {
                      moveToStep(yourmarker,yourroute,stopTime);
                  }
              },interval);
          }
          else {
              window.setTimeout(function(){
                  moveToStep(yourmarker,yourroute,c+1);
              },interval);
          }
      }
  }
  function str_pad_left(string,pad,length) {
      return (new Array(length+1).join(pad)+string).slice(-length); 
  }
  function jumpTo(value) {
      alert(value);
      moveToStep(marker,lat,100);
  }
  function initialize() {
    var mapOptions = {
      center: new google.maps.LatLng(31.26,121.45),
      zoom: 12,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        mapOptions);
    var marker = new google.maps.Marker({map: map,});
    var newLatLng = new google.maps.LatLng(lat[0],lon[0]);
    marker.setPosition(newLatLng);
    moveToStep(marker,lat,0);
  }

Right now, I have two buttons, one which calls the clickPause() function, and one that calls the clickPlay() function. 现在,我有两个按钮,一个调用clickPause()函数,另一个调用clickPlay()函数。 The pausing works fine, but when I try to resume, the marker exhibits some very strange behavior. 暂停效果很好,但是当我尝试恢复时,标记器表现出一些非常奇怪的行为。

Essentially, it seems that the marker is jumping back to the position where it was last paused, then quickly jumping forward again, and doing this every time the marker position updates (which is once per 1000 milliseconds, or once per second, as set by the interval variable.) 从本质上讲,标记似乎在跳回到上次暂停的位置,然后又迅速向前跳,并且每次标记位置更新时都执行此操作(按每千毫秒一次或每秒一次,由interval变量。)

Has anyone seen this type of behavior before? 有没有人看过这种行为? I don't understand what is wrong with my logic in these lines, which I'm sure is the culprit: 我不明白这些行的逻辑出了什么问题,我确定是罪魁祸首:

          if(paused == 1) {
              stopTime = c+1;
              window.setInterval(function(){
                  if(paused == 0) {
                      moveToStep(yourmarker,yourroute,stopTime);
                  }
              },interval);
          }
          else {
              window.setTimeout(function(){
                  moveToStep(yourmarker,yourroute,c+1);
              },interval);
          }

All I'm doing is checking if the simulation is paused, and if it is, check if it has been unpaused, then once it is, resume the movement of the marker. 我正在做的只是检查模拟是否已暂停,如果已暂停,请检查它是否已暂停,然后一旦恢复,就可以继续移动标记。 Once I have pressed the resume button, the code should go straight to 一旦按下恢复按钮,代码应直接转到

              window.setTimeout(function(){
                  moveToStep(yourmarker,yourroute,c+1);
              },interval);

because the value of paused has been returned to 1 . 因为已paused的值已返回1

Can anyone help me out? 谁能帮我吗?

You need to clear the interval in order to stop it from firing after you resumed. 您需要清除时间间隔,才能在恢复后停止触发。 This means that you need to store the identifier returned by setInterval and pass it to clearInterval when needed. 这意味着您需要存储setInterval返回的标识符,并在需要时将其传递给clearInterval

var pauseInterval; // interval identifier (outside the moveToStep function)

if (paused == 1) {
    stopTime = c+1;
    pauseInterval = setInterval(function() {
        if(paused == 0) {
            moveToStep(yourmarker, yourroute, stopTime);
        }
    }, interval);
} else {
    clearInterval(pauseInterval);
    setTimeout(function() {
        moveToStep(yourmarker, yourroute, c+1);
    }, interval);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM