CallbackProperty function with arguments?

868 vues
Accéder directement au premier message non lu

Matt Whalley

non lue,
8 sept. 2016, 19:36:2508/09/2016
à 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

non lue,
9 sept. 2016, 10:04:1209/09/2016
à 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

non lue,
9 sept. 2016, 10:31:0109/09/2016
à 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

non lue,
14 sept. 2016, 17:23:4014/09/2016
à 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


Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message