Calculate the viewing location in Cesium
Here I summarise two approach for calculating the viewing location in Cesium viewer:
Method 1 (using Camera.computeViewRectangle() method)
var projection = new Cesium.WebMercatorProjection();
var rect = new Cesium.Rectangle()
viewer.camera.computeViewRectangle(projection.ellipsoid, rect)
let rect_x = (rect.east - rect.west)/2 + rect.west;
let rect_y = (rect.north - rect.south)/2 + rect.south;
var rect_lon = Cesium.Math.toDegrees(rect_y);
var rect_lat = Cesium.Math.toDegrees(rect_x);
console.log(rect_lat, rect_lon)
Note: The computeViewRectangle( ) could be very large when the view pitch is very flat, resulting in a very far point being calculated.
Method 2 (using Camera.computeViewRectangle() method)
var cc = document.getElementById("cesiumContainer");
var screenCenterPt = new Cesium.Cartesian2(cc.offsetWidth/2, cc.offsetHeight/2);
var pick = viewer.scene.globe.pick(viewer.camera.getPickRay(screenCenterPt), viewer.scene);
if (pick) {
var geoPt = viewer.scene.globe.ellipsoid.cartesianToCartographic(pick);
var point = [geoPt.longitude/ Math.PI * 180, geoPt.latitude/ Math.PI * 180];
var rect_lon = point[0];
var rect_lat = point[1];
console.log(rect_lat, rect_lon);
}
Note: It uses screen center to calculate the point, seems to be more useful.
Comments
Post a Comment