# Changing points of a polyline dynamically

**URL:** <https://community.cesium.com/t/changing-points-of-a-polyline-dynamically/33524>\
**Category:** CesiumJS\
**Created:** [July 4, 2024, 7:21am UTC](https://community.cesium.com/t/changing-points-of-a-polyline-dynamically/33524 "2024-07-04T07:21:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yordan\_Yanakiev1](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/yordan_yanakiev1/32/2128_2.png) [@Yordan\_Yanakiev1](https://community.cesium.com/u/Yordan_Yanakiev1)\
**Post date:** [July 4, 2024, 7:21am UTC](https://community.cesium.com/t/changing-points-of-a-polyline-dynamically/33524/1 "2024-07-04T07:21:22Z")

</div>

Hello.  
I am trying to change the points of a polyline dynamically, but not happening by some reason, and I am unable to find a demo which describing how to.  
I have a points which is coming each 1-2 seconds from a server, and I need to add them to a single polyline.  
How to do that ?

---

<div class="post-metadata">

**Author:** ![anneg](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/anneg/32/11217_2.png) [@anneg](https://community.cesium.com/u/anneg)\
**Post date:** [July 5, 2024, 10:47am UTC](https://community.cesium.com/t/changing-points-of-a-polyline-dynamically/33524/2 "2024-07-05T10:47:58Z")

</div>

> but not happening by some reason,

Please provide a [Sandcastle](https://sandcastle.cesium.com/) to show what exactly is not happening, containing code that shows what you’ve tried so far.

---

<div class="post-metadata">

**Author:** ![Yordan\_Yanakiev1](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/yordan_yanakiev1/32/2128_2.png) [@Yordan\_Yanakiev1](https://community.cesium.com/u/Yordan_Yanakiev1)\
**Post date:** [July 6, 2024, 3:49pm UTC](https://community.cesium.com/t/changing-points-of-a-polyline-dynamically/33524/3 "2024-07-06T15:49:15Z")

</div>

Well I am trying to use polyline.positions as an Array, and add to it more points, but it is not an array and it just not working, and I am unable to find an example which is doing just this - to add more points to a polyline, so i am not sure what to write and test in this sandcastle.

---

<div class="post-metadata">

**Author:** ![Marco13](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/marco13/32/2757_2.png) [@Marco13](https://community.cesium.com/u/Marco13)\
**Post date:** [July 6, 2024, 4:31pm UTC](https://community.cesium.com/t/changing-points-of-a-polyline-dynamically/33524/4 "2024-07-06T16:31:30Z")

</div>

You can define a `Property` that returns the desired positions.

> **[Cesium Sandcastle](https://sandcastle.cesium.com/#c=hZNRb5swEMe/yoknKlEnpJs0pSRal1bbQ7dVXdaX0QcXLok1YyPbJKIT330HJlWU0PYB8B2/u/vf2c60sg62AndoYAYKd7BAK6qCPXS+MA2yzl5o5bhQaNLg7DJVqcq6yFJb4QQtr3FtEO2VMbymRH/O44txBO1zHk/o/WH8eHkSdGd0ica1Af9SBSDsogW4clNYcWkxar1rdA9cVki+SmVtYHjmeQCDrjJqr3nBjaMVVxdsZXRxqCkcVNq2AtCkqjkQJ+tbapQ0+bEwVI4i0TKe52FXt2UkMdO9jJfk09PmIo/sRO42U/jYm9xky7qkDL30K2+z+2+/v3/pmYI7NILLF2ihpTbs6/3NzY8Oaejd+O1IVa/2WetiqcMj7R7azw+okzstlAulVjSPCCR39O3HOjgqVlZ20/N+bG9hPh1hDcBA3WuUvMb8qHwEhe0lWHRLUaCuXEibPZvvB/2acL+PXYJXi9r9sTlW4U9oe1bj8Xjscw0wnzwzeYuJiem4d5hJeyM804k90EiuIAoS62qJc981wGdRlNo4qIwMGRs5LErqHO3oqcr+omOZtb4eQDI6DE1ysQWRzwbuMWSSW0t/VpWUv8QzpsE8GRF/Eio1z4Va/9yioS5abBPPb72TMZaMyByOdFrLJ26OMv8H)**
>
> The Cesium Sandcastle provides an interactive environment for testing Cesium code.

---

<div class="post-metadata">

**Author:** ![Yordan\_Yanakiev1](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/yordan_yanakiev1/32/2128_2.png) [@Yordan\_Yanakiev1](https://community.cesium.com/u/Yordan_Yanakiev1)\
**Post date:** [July 8, 2024, 6:33am UTC](https://community.cesium.com/t/changing-points-of-a-polyline-dynamically/33524/5 "2024-07-08T06:33:20Z")

</div>

Thank You Marco13 ! This is really going forward help.  
I have the follow issue now, and I am not understanding the problem.  
I encapsulating everything in classes, and I have the follow class:

```auto
class Trajectory
{
    positionsHolder = undefined;
    constructor( )
    {        
       
        this.positionsHolder = new Array( 0, 0, 1, 100 );
    }
    get positionsProperty() 
    {
        console.log(this.positionsHolder);

        return Cesium.Cartesian3.fromDegreesArray(this.positionsHolder);

    }
    trajectoryDescriptor =
    {
        type: 'Trajectory',
        polyline:
        {
            positions: this.positionsProperty,

            width: 4,
            material: Cesium.Color.RED,
            clampToGround: true
        }
    }
}

```

But I keep getting that error Warning: Unhandled exception rendering component: coordinates is required, actual value was undefined  
**positionsProperty** seems like return undefined.  
Outside class works, but not inside class. Is there something else I shall consider when using it encapsulated ?

---

<div class="post-metadata">

**Author:** ![Marco13](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/marco13/32/2757_2.png) [@Marco13](https://community.cesium.com/u/Marco13)\
**Post date:** [July 8, 2024, 10:47am UTC](https://community.cesium.com/t/changing-points-of-a-polyline-dynamically/33524/6 "2024-07-08T10:47:32Z")

</div>

When you post a Sandcastle where it is possible to reproduce the error, it can probably be solved quickly.

---

<div class="post-metadata">

**Author:** ![Yordan\_Yanakiev1](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/yordan_yanakiev1/32/2128_2.png) [@Yordan\_Yanakiev1](https://community.cesium.com/u/Yordan_Yanakiev1)\
**Post date:** [July 8, 2024, 11:36am UTC](https://community.cesium.com/t/changing-points-of-a-polyline-dynamically/33524/7 "2024-07-08T11:36:32Z")

</div>

> **[Cesium Sandcastle](https://sandcastle.cesium.com/#c=hVRNb9swDP0rQi51AE/56mFIk2BDMmyHAivWYCdfVJtJtMmSIdEJ3CL/fbRlx4mTdoIAy/TjI/lIOTbaIdtLOIBlc6bhwJbgZJ7y35UtiHpx9b40GoXUYKNe/yHSkY6VcI6trfgDMRpbRPot0oxWZpxESbw/jEoq1lwnsCHf5MEj4jKozUu3gPW97Y1dLm8tF+6k49ekZapfrRVFwIZhuUe0h0PWr4McyyTLwxawzenJmgwsFkG/jvDWBrKAudVN/UthkU5CT/jGmnQFWwvgfMRbKV3FxZM0K3CxlRkd2fwqKhYZTNldK+Rd2H7MjCoUKTdtTWeuF2pPO0I1lYZNPs06yAR3U3YfXppTgWClUNOTAEYZy399W3WA1Pg0W5vv1lBfKarNoQUcTyLQVqR8q0Lds7bQoB6kagTLSh+pUkL5aeSgkSoBx0WSBGc8/KawnivStfOrMenaBB0qD9rkOi4lYkT8ZKTGQBlN/Q2ZEkjPvtf4pOR563mWu12N9x3/CObpCHYsB/oq7gqUKCDphA9Z6uoUHOBapmByDGhg54um+e8lXiV0rAjeDeqCmrybxafRmG7RZFhdo6HnuoH57DHjjzB0FScV7j+Yccjua0yV7FmOZOqFvZnDQsGima8vMs2MRZZbFXA+QEgzqhzc4CWP/wLy2LnmGs4G566zRO6ZTOY3/mas+pHRl02u1LN8hai3mA0If+WqjEik3v7cg6UqSthutHj0Rs75bECvtz3RGPUibIf5Hw)**
>
> The Cesium Sandcastle provides an interactive environment for testing Cesium code.

---

<div class="post-metadata">

**Author:** ![Marco13](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/marco13/32/2757_2.png) [@Marco13](https://community.cesium.com/u/Marco13)\
**Post date:** [July 8, 2024, 12:08pm UTC](https://community.cesium.com/t/changing-points-of-a-polyline-dynamically/33524/8 "2024-07-08T12:08:38Z")

</div>

Adding a log output like

```javascript
    get positionsProperty() 
    {
        console.log("this.positionsHolder is "+this.positionsHolder);
        return Cesium.Cartesian3.fromDegreesArray(this.positionsHolder);
    }

```

or adding a  
`debugger;`  
statement there or or looking at the stack trace (or even just _posting_ the stack trace as part of the error description!) or knowing the quirks of JavaScript could have answered that.

I do _not_ know the quirks of JavaScript 🙂  
But I’ve learned something today, and isn’t that great? 🎓

The stack trace is

```auto
DeveloperError: coordinates is required, actual value was undefined
Error
    at new DeveloperError (https://sandcastle.cesium.com/CesiumUnminified/Cesium.js:10462:13)
    at Check.defined (https://sandcastle.cesium.com/CesiumUnminified/Cesium.js:10498:13)
    at Cartesian3.fromDegreesArray (https://sandcastle.cesium.com/CesiumUnminified/Cesium.js:11337:19)
    at get positionsProperty (<anonymous>:16:34)
    at <instance_members_initializer> (<anonymous>:24:29)
    at new Trajectory (<anonymous>:9:16)
    at window.startup (<anonymous>:32:18)
    at <anonymous>:60:12
    at HTMLScriptElement.loadScript (https://sandcastle.cesium.com/CesiumSandcastle.js:681:24) (on line 13)

```

Now, it says that the error comes from  
`at <instance_members_initializer> (<anonymous>:24:29)`

This can **roughly** be imagined as follows:

- It creates the `Trajectory` object
- As part of this object, it initializes the `trajectoryDescriptor`
- This happens **before running the `constructor()`**. This means that the `positionsHolder` is still `undefined` at this point!
- As part of the `trajectoryDescriptor`, it tries to get the `positionsProperty`, which uses the `positionsHolder` (which is still `undefined`)
- Bam ⚡ - it crashes

There are different possible solutions. A simple one is to not initialize the `trajectoryDescriptor` as an “instance member”, but to just return it from a getter (assuming that this will be called only once - if not, you have to solve this differently).

Note that what you did there is still not _dynamic_. You are still just returning a positions array, and **not** a `Property` that returns the positions array. In order to return a property, you have to… return a property. This can be done as shown in my first answer here, or using a `CallbackProperty`. Another example, including a fix for the error that you mentioned:

> **[Cesium Sandcastle](https://sandcastle.cesium.com/#c=fVRNb9swDP0rRC51AU9Jkx6GfGFDMmyHAiu2YCdfVJtJtMqSIckp3KH/fbTlj8QxYgiwTT1Sj+SjYq2sg5PANzSwAoVvsEEr8pT9qWxBNIqr/41WjguFJhrdLyIVqVhya2Fn+F+MnTYF/IsUQFzGM3lpCe69CcAdhWWZtsIJ2v6hZdIe9tUYXgSTEGg90JpMyugAH+URAAd00Do+G52hcUUX2KDLjTpnveFSvvD4tcUSeLVu8K1HizaOvriasb3R6RYPBtF6TkOkPTdiF8KeS4sXXHmSPNfwQGpFwUKQ3NH7ZiFYlttj7dDEv4HzAfs1cm0btmhjI7KL+ldNGcTAyiPa+rgiwzncdW29C5utTMtCkgLmjaF1gq5H8x73pg+h5+qfN5G44xwew86UcodGcDlvW6OlNuzXt+0ZiCSXZjv93ehcJXSSybHZ/Kgbs7gQxlDKTelo9etSa7JLPqilXuHK/J8of0L5eWGoHGWJllHrgy4MGzq2jFS7vWud7nTQC+KP2ucqLgvn1SQUdVvyApOeokJIbd1ei24nUtS5u9T6GZ8byvTVqMItqppcEbCNjvqMPk1pZmeTamjrqb2GfPaQ6Q0Ijf2sgt2GTEN4rCFE84wdGUbhaGldIXHd6OGLSDNtHORGBoyNHaYZZYx2/JLHr+hYbG0zbMvxuesyEScQyWrg3oPqyqOdfS7lb/GO0Wi9HBP+ylVqngh1+HlCQymUsOPD+skbGWPLMf0Oezqt6f4yvcj/AQ)**
>
> The Cesium Sandcastle provides an interactive environment for testing Cesium code.

Also note that dynamic polylines currently have a distressingly low rendering performance. This is “tracked” in [Dynamic Buffers · Issue #932 · CesiumGS/cesium · GitHub](https://github.com/CesiumGS/cesium/issues/932) but has not … “yet”… been addressed.

---

<div class="post-metadata">

**Author:** ![Yordan\_Yanakiev1](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/yordan_yanakiev1/32/2128_2.png) [@Yordan\_Yanakiev1](https://community.cesium.com/u/Yordan_Yanakiev1)\
**Post date:** [July 8, 2024, 1:30pm UTC](https://community.cesium.com/t/changing-points-of-a-polyline-dynamically/33524/9 "2024-07-08T13:30:19Z")

</div>

Good heavens ! This wouldn’t pass my mind as approach soon.  
Now sure how to thank You.  
So… THANK YOU ! \<3
