CallbackProperty function with arguments?

How do I pass an argument to a CallbackProperty function?

This current code that works (acState is a global variable):

acEntity = viewer.entities.add({

** position: new Cesium.CallbackProperty(getAcPosition, false),**

model : {

uri : acHref,

}

});

function getAcPosition() {

return Cesium.Cartesian3.fromDegrees(acState.lng, acState.lat, acState.alt + undulation);

}

But what I’d like to do is pass the variable acState as an argument.

acEntity = viewer.entities.add({

** position: new Cesium.CallbackProperty(getAcPosition(acState), false),**

model : {

uri : acHref,

}

});

function getAcPosition(state) {

return Cesium.Cartesian3.fromDegrees(state.lng, state.lat, state.alt + undulation);

}

Obviously I can’t just pass an argument to the callback function. I get the error “object is not a function”.

Thanks in advance.

  • Matt

Hello Matt,

Sorry, there currently isn’t a way to pass an argument to the callback function. Using a global variable is an okay solution. Another thing you can try is function scoping an argument like this:

function getCallbackFunction(argument) {
return function callbackFunction() {
return Cesium.Cartesian3.fromDegrees(argument.lon, argument.lat);
};
}

viewer.entities.add({
position: new Cesium.CallbackProperty(getCallbackFunction(argument), false)
});

``

Best,

Hannah

This is a general JavaScript question, and not anything specific to Cesium, so keep that in mind when searching around for information. Hannah’s code is essentially the same as Function.bind which you might find more convenient.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

function getPosition(state) {

return …

}

new CallbackProperty(getPosition.bind(undefined, someValue), false)

This would also extend to a version that uses the complete signature of the callback property:

function getPositon(state, time, result) {

}

Hi Matt,

What are you working on? As the editor for Cesium’s development team, I’m always looking for good applications we can highlight on our showcases page: http://cesiumjs.org/demos.html Is your project in a position to demo on our site?

Regards,

Sarah