Polygon insertPoint with setTimeout

847
4
02-01-2019 03:31 PM
ShadCampbell
New Contributor III

Trying to insert a point into a polygon graphic on a time delay using setTimeout.  The code works as expected if I insert the point immediately when the page loads, but if I wait three seconds nothing draws, but the data has been updated.  This JSFiddle example has a function called start.  When I call the function directly it works, but when I call it after a 3 seconds delay using setTimeout it does not update the graphic.  Is this a bug or am I missing something?

Also, if I change the time to 700 milliseconds it works... 900 it does not.  In between it works randomly. 

function start() {
    polylineGraphic.geometry.insertPoint(0, 0, [-112.3, 53.68]);
}

start(); // Works
//setTimeout(function () { start(); }, 3000); // Does not work.

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus

Shad,

   This is the way to do it:

      // Works
      setTimeout(function () {
        view.graphics.remove(polylineGraphic);
        var gClone = polylineGraphic.clone();
        gClone.geometry.insertPoint(0, 0, new Point({x:-112.3, y:53.68}));
        view.graphics.add(gClone);
        console.info(gClone);
      }, 3000);
0 Kudos
ShadCampbell
New Contributor III

Thanks Robert, 

I'm actually REALLY trying to not have to clone, alter, and re-add the object.  My sample code is to simply show the error, however what I'm really trying to do is automate the drawing of a graphic on the screen.  similar to a car driving around New York.  I'm actually using setInterval, not setTimeout... but the issue is the same.  Since I know the distance and rate of travel I can determine the time (or milliseconds) it will take to move along the line, I can set the interval time to approximately match the true MPH of the car on the road.  My issue with cloning the graphics object  is that it's VERY expensive in processing time and throws off my calculation by adding to the milliseconds.   Would be a lot faster processing time if I could just update the existing geometry.  

-Shad

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Shad,

   I am not sure if it has changed in the API but in the past you could not just update a graphics geometry you had to remove and re-add it to get it to update.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Shad,

   OK I checked and the compatibility matrix still says this:

Modify graphicsComing soonTo modify a graphic that is already added to the layer: you need to clone it, make modifications, add it to the layer and remove the original graphic. In-place modification of graphic objects will not trigger display refresh.
0 Kudos