Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get center in Cesium Map

Tags:

gis

cesium

I need to know what is the current center of the Cesium Map.

I tried to use viewer.camera.position but it always gives the same z value (x: 16921255.101297915, y: 5578093.302269477, z: 12756274) and I'm not sure about the x and y values. Are they in meters?

Thanks a lot!

EDIT: Solution

With all the help I got (thanks!) I put this together:

getPosition(){
    if (viewer.scene.mode == 3) {
        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 * (180 / Math.PI));
        console.log(pickPositionCartographic.latitude * (180 / Math.PI));
    } else if (viewer.scene.mode == 2) {
        var camPos = viewer.camera.positionCartographic;
        console.log(camPos.longitude * (180 / Math.PI));
        console.log(camPos.latitude * (180 / Math.PI));
    }
};

This function gives longitude/latitude coordinates in degrees.

like image 447
Pri Santos Avatar asked Oct 26 '15 14:10

Pri Santos


1 Answers

viewer.camera.position gives you the position at which camera is located in X,Y,Z coordinates in meters in relation to earth center.

Depending on which scene mode you are using approach is different:

SCENE3D:

In order to see at what is the camera looking at you need to get the intersect point of camera's pick ray and map.

 function getMapCenter() {            
        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 * (180/Math.PI));
        console.log(pickPositionCartographic.latitude * (180/Math.PI));
}

Based on this thread.

Also try to check if camera is looking at the map, and not a the sky.

SCENE2D:

This is a simple 2D view with camera pointing directly down.

From docs:

2D mode. The map is viewed top-down with an orthographic projection

var camPos = viewer.camera.positionCartographic; 
console.log(camPos.longitude * (180/Math.PI)); 
console.log(camPos.latitude * (180/Math.PI));

Remaining case is 2.5D or COLUMBUS_VIEW

like image 99
Tomislav Muic Avatar answered Oct 02 '22 10:10

Tomislav Muic