Get the center on a cesium map

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

I tried to use viewer.camera.position , but 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 received (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 the longitude / latitude coordinates in degrees.

+5
source share
1 answer

viewer.camera.position gives you the position in which the camera is in X, Y, Z coordinates in meters relative to the center of the earth.

Depending on which story mode you are using, the approach is different:

Scene3D:

To see what the camera is looking at, you need to get the intersection point of the camera, which selects the beam 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 checking if the camera is looking at the map, not the sky.

SCENE2D:

This is a simple 2D view with a straight down camera.

From the docs:

2D mode. Map viewed from top to bottom with spelling

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

Remaining 2.5D case or COLUMBUS_VIEW

+3
source

Source: https://habr.com/ru/post/1234534/


All Articles