HNipps HNipps - 3 years ago 141
Javascript Question

CesiumJS: Draggable points jump to static point locations

I've written a few functions to draw draggable and static points in Cesium. However, I have a big issue when using these together.

I can draw draggable points by clicking on the map, and move them around by clicking and dragging. But if I plot a static point after plotting a draggable point, the draggable point jumps to the location of the static point! I can still drag the point, but it always snaps back to the static points location.

Here's a Sandcastle demo to illustrate my problem - http://cesiumjs.org/Cesium/Apps/Sandcastle/?src=Hello%20World.html&label=Showcases&gist=54a2252d95768ca89d23948ad010356a

In this example, any click on the map will create a draggable point (red points). There are two timeouts at the end of the script, one of which will draw a static point after 5s, and the other will plot a static point at a different location after 10s. The static points are purple and a lot smaller than the draggable ones.

As you'll see, if you draw a draggable point in the first 5s, it will jump to the location of the first static point.

What's going on?!

Answer Source

Rachel Hwang from the Cesium team solved this for me. Here's her solution:

This:

 let positionCallback = () => {

     return waypointPosition;

};

Should be:

 let positionCallback = (time, result) => {

    return waypointPosition.clone(result);

};

The reason is that the signature for the CallbackProperty callback (https://cesiumjs.org/Cesium/Build/Documentation/CallbackProperty.html#~Callback) takes a time and result. If result is provided then you MUST store the result into that existing object and return it (otherwise you should return a new value). Calling clone(result) does just that. The reason it is done this way is because we make heavy use of scratch variables to avoid memory allocations.

The Sandcastle now works as required - http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Hello%20World.html&label=Showcases&gist=54a2252d95768ca89d23948ad010356a

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download