Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:35:15+00:00 2026-06-17T22:35:15+00:00

I’m currently working with the Google Maps V3 API, and I’m animating a marker

  • 0

I’m currently working with the Google Maps V3 API, and I’m animating a marker to travel a predetermined route. The route information is stored in two separate arrays, lat for the latitude coordinates, and lon for the longitude coordinates. 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. 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.)

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.

Can anyone help me out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:35:17+00:00Added an answer on June 17, 2026 at 10:35 pm

    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.

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from
Let's say I'm outputting a post title and in our database, it's Hello Y’all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.