ScreenSpaceEventHandler

I am attempting to replicate the functionality of drawing lines on the terrain that is found in this Sandcastle example.

However, the only time points or lines are drawn is when my cursor is over an object that has been added to the scene such as a model, label, etc. When the cursor is just over the globe the events don’t work.

I’m convinced there’s a simple reason for this, but I’m not sure what to look for. Why would these listeners only fire when the cursor is over objects in the scene and not the globe itself?

UPDATE:
I discovered that the listeners are firing just fine. It’s actually the value being returned from viewer.scene.pickPosition that’s the issue. It’s not returning the position under the cursor unless the cursor is over an object that has been added to the scene. Instead… if the cursor is just over the globe then the position being returned is quite a distance from the cursor. Any ideas about why this is happening would be appreciated.

Is viewer.scene.pickPosition the best way to get the most accurate point on the ground?

I think it can depend on which ‘version’ of pickPosition you use. When moving over the globe i use the event.EndPosition inside the function (from handler.setInputAction((event) => {})); when clicking, i use event.position.

Thanks, jos…

I’m interested to know if viewer.scene.pickPosition is the best approach for getting an accurate location on the ground as a result of a click of the mouse. I had difficulty getting this to work, so I ended up using:

var pickRay = scene.camera.getPickRay(event.position);
var earthPosition = scene.globe.pick(pickRay, scene); //returns a cartesian3 at the mouse position whether there is terrain or not

It seems there are several approaches. I’d like to understand when to use approach and why?

There are a couple of longstanding bugs with picking points on the surface depending on what map mode you’re in (2D/3D/Columbus). You can follow these on GitHub if you’re interested:

I wound up with a solution similar to yours, except using scene.camera.pickEllipsoid. The last link above shows some of the code I’m using.

James_B thanks for the response and the links. These are helpful to read through.

This topic (best way to get the click location) seems to be a very common question with a big variety of answers.

I wish one of the Cesium guys would take this topic and summarize a “best practices” answer…