Changing the style of newly added entities (GeoJsonDataSource).

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

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;

});

Thanks, that did it!