Using of scene.pick

HI
I have to following code.

viewer.screenSpaceEventHandler.setInputAction(function onMouseMove(movement) {
            // Pick a new feature
            var pickedFeature = viewer.scene.pick(movement.endPosition);
            if (!Cesium.defined(pickedFeature)) {
                console.log("No picked feature");
                return;
            }

           console.log(pickedFeature.primitive.id);
}

The output of console.log(pickedFeature.primitive.id); is object Object

How do I know what is type of instance is the pickedFeature, is it a Cesium.Entity, Cesium.Billboard?
I have bilboards and polylines, that was created from code and some was loaded via a czml file.

Please help, I would ike to log out some of the properties of picked features, and later on display it.

The documentation of Scene#pick remains a bit vague here - and to some extent, this is intentional: There are many possible types of objects that may appear in the scene.

When you mention the console output that just says [object Object], then this refers to the built-in console of the Sandcastle. But you can receive more detailed information in the console of the browser. You can look at this console in the “Developer Tools” of the browser. These tools can be opened through the menu, but in Chrome or Firefox you can just hit the F12 key.

Here is an example of the Sandcastle console at the top, and the browser developer tools console at the bottom, showing the result of console.log(pickedFeature.primitive); in an example where a Billboard is picked:

You can see that you’ll receive detailed information about the type of the picked object, and even browse through its structure or properties.

Depending on which objects you want to pick, you can check the type using instanceof. This can be done like this:

const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function (movement) {
  const pickedFeature = viewer.scene.pick(movement.endPosition);
  if (!Cesium.defined(pickedFeature)) {
    console.log("No picked feature");
    return;
  }
  console.log(pickedFeature.primitive);
  if (pickedFeature.primitive instanceof Cesium.Billboard) {
    console.log("That's a billboard");
  } else if (pickedFeature.primitive instanceof Cesium.Primitive) {
    console.log("That's a generic primitive (maybe from a CZML)");
  }
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

(An aside: I usually try to avoid the use of instanceof. But in this case, there is no way around that: You simply do not know the types of all the objects that may appear in the scene, so you have to check for the type explicitly…)

1 Like