CallbackProperty function with arguments?

662 views
Skip to first unread message

Matt Whalley

unread,
Sep 8, 2016, 7:36:25 PM9/8/16
to cesium-dev
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

Hannah Pinkos

unread,
Sep 9, 2016, 10:04:12 AM9/9/16
to cesium-dev
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

Scott Hunter

unread,
Sep 9, 2016, 10:31:01 AM9/9/16
to cesiu...@googlegroups.com
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.


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) {
   
}


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sarah Chow

unread,
Sep 14, 2016, 5:23:40 PM9/14/16
to cesium-dev
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


Reply all
Reply to author
Forward
0 new messages