Changing the style of newly added entities (GeoJsonDataSource).

315 views
Skip to first unread message

Alessio Pierluigi Placitelli

unread,
Sep 19, 2014, 11:09:49 AM9/19/14
to cesiu...@googlegroups.com
Hi all,

I'm trying to change the style of an entity (change its billboard and adding some text) as soon as it changes because of some new data coming from the DataSource (GeoJson in this case, slightly
modified to make getOrCreateEntity get the correct Id and to allow to add new entities without removing the previous ones, like the CzmlDataSource).

My code looks like this:

dataSource.entities.collectionChanged.addEventListener(
          function collectionChangedEventCallback(collection, added, removed, changed){

              for (var i = 0; i < added.length; i++) {
                  var entity = added[i];

                  var style = _getStyle(entity);

                  entity.point = style[0];
                  entity.billboard = style[1];
              }
          });

As soon as it calls the entity.point .. line, it triggers another collectionChanged event with an "added" count equal to 1. I understand
that collection.suspendEvents() and collection.resumeEvents() can be used to mitigate this issue. Unfortunately, the problem is the same since resumeEvents() triggers another collectionChanged
event.

Is there any way to achieve my objective?

Alessio


Scott Hunter

unread,
Sep 19, 2014, 3:04:02 PM9/19/14
to cesiu...@googlegroups.com
You could modify your callback function to detect re-entrancy and ignore the events that are triggered from inside your callback:


var inCallback = false;
dataSource.entities.collectionChanged.addEventListener(
          function collectionChangedEventCallback(collection, added, removed, changed){
              if (inCallback) {
                return;
              }
              inCallback = true;
              collection.suspendEvents();
              for (var i = 0; i < added.length; i++) {
                  var entity = added[i];

                  var style = _getStyle(entity);

                  entity.point = style[0];
                  entity.billboard = style[1];
              }
              collection.resumeEvents();
              inCallback = false;
          });


--
You received this message because you are subscribed to the Google Groups "cesium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cesium-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alessio Pierluigi Placitelli

unread,
Sep 22, 2014, 11:10:09 AM9/22/14
to cesiu...@googlegroups.com
Thanks, that did it!
Reply all
Reply to author
Forward
0 new messages