How to get globe center point?

Is there a way to find out the center lat/lon of the user's view in 3D? So as they spin the globe, wherever the camera's focus point would be, can we get that position? I've tried working with getting the extent with some code on the forums, but it doesn't work if the sky is in view. My use case isn't as complicated as needing the full extent, I just need the center point of focus. Anyone have experience with this?

Hi,

Something like this should do the trick:

var windowPosition = new Cesium.Cartesian2(viewer.container.clientWidth / 2, viewer.container.clientHeight / 2);

var pickRay = viewer.scene.camera.getPickRay(windowPosition);

var pickPosition = viewer.globe.pick(pickRay, viewer.scene);

var pickPositionCartographic = viewer.globe.ellipsoid.cartesianToCartographic(pickPosition);

Kevin

Wow that was quick...Works perfectly, thanks Kevin!

Kevin,

This doesn't seem to be working anymore, is there any updated method?

Looks like Viewer doesn’t have a reference directly to globe anymore, so you need to get it via scene. The code should look like this now:

var windowPosition = new Cesium.Cartesian2(viewer.container.clientWidth / 2, viewer.container.clientHeight / 2);

var pickRay = viewer.scene.camera.getPickRay(windowPosition);

var pickPosition = viewer.scene.globe.pick(pickRay, viewer.scene);

var pickPositionCartographic = viewer.scene.globe.ellipsoid.cartesianToCartographic(pickPosition);

While this does produce a lat/lon, it picks 0.9766109323242717, -0.05579396331590568 (centre of the Atlantic) irrespective of actual viewport bounds, so there seems to be a disconnect between viewer.container and the actual zoomed location(?). I can provide a sandcastle example if necessary.

Hmm not seeing that here. Here’s my (rather lame) Sandcastle example:

var viewer = new Cesium.Viewer(‘cesiumContainer’);

setTimeout(function() {

var windowPosition = new Cesium.Cartesian2(viewer.container.clientWidth / 2, viewer.container.clientHeight / 2);

var pickRay = viewer.scene.camera.getPickRay(windowPosition);

var pickPosition = viewer.scene.globe.pick(pickRay, viewer.scene);

var pickPositionCartographic = viewer.scene.globe.ellipsoid.cartesianToCartographic(pickPosition);

console.log(pickPositionCartographic.longitude);

console.log(pickPositionCartographic.latitude);

}, 2000);

Did you look at the outputs that example is giving, though? Eg if I set the timeout to 10 seconds, and then zoom in on London, it gives these coordinates:

0.8991555316647265, -0.0017840120829458162

Which is obviously in the centre of the Atlantic, the default view.

Ah, scratch that - simple mistake. It was outputting lat/lon in radians, not degrees. Cheers. Only other question is how to trigger events every time the view is changed (ie zooming or scrolling from the user).

You can listen to the camera.moveStart and moveEnd events for that. Here’s a simple example from our Google Earth ports: http://analyticalgraphicsinc.github.io/cesium-google-earth-examples/examples/viewchangeEvent.html

This is great, but it doesn't seem to be working with 1.9 - I had to downgrade due to bugs in 1.11

Hi,
The code doesn't seem to be working anymore, is there any updated method?
i have DeveloperError: cartesian is required.
i want to get the globe center point coordinates (lat/lon) when it rotates/moves action

Thanks

Hello,

This should work:

var windowPosition = new Cesium.Cartesian2(viewer.container.clientWidth / 2, viewer.container.clientHeight / 2);
var pickPosition = viewer.camera.pickEllipsoid(windowPosition);
var pickPositionCartographic = viewer.scene.globe.ellipsoid.cartesianToCartographic(pickPosition);

``

Best,

Hannah

Hi,
I have the same error: DeveloperError: cartesian is required.
Cartesian2 {x: 716.5, y: 0}
     x: 716.5 y: 0
     __proto__: Cartesian2
undefined
Uncaught DeveloperError: cartesian is required.