Cesium获取当前视图范围四至坐标

Cesium获取当前视图范围四至坐标_第1张图片

在Cesium中
三维的视图范围可通过viewer.camera.computeViewRectangle()获取;
2维和2.5维需要camera.pickEllipsoid接口,将cesium场景绘图区屏幕坐标转化为世界坐标,只需要输入当前场景左上角屏幕坐标(0,0)和右下角屏幕坐标(canvas.width, canvas.height)即可获取对应的世界坐标值,然后再使用ellipsoid.cartesianToCartographic接口将世界坐标转化为经纬度坐标


const extend = viewer.camera.computeViewRectangle();
const params = {}
if (typeof extend === "undefined") {
    const coordToLonlat = (viewer, x, y) => {
    	const { camera, scene } = viewer
    	const d2 = new Cesium.Cartesian2(x, y);
   		const ellipsoid = scene.globe.ellipsoid;
    	//2D转3D世界坐标
    	const d3 = camera.pickEllipsoid(d2, ellipsoid);
    	//3D世界坐标转弧度
    	const upperLeftCartographic = scene.globe.ellipsoid.cartesianToCartographic(d3);
    	//弧度转经纬度
    	const lon = Cesium.Math.toDegrees(upperLeftCartographic.longitude);
    	const lat = Cesium.Math.toDegrees(upperLeftCartographic.latitude);
    	return { lon, lat }
	}
    const canvas = viewer.scene.canvas;
    const upperLeftLonLat = coordToLonlat(viewer, 0, 0);
    const lowerRightLonLat = coordToLonlat(viewer, canvas.clientWidth, canvas.clientHeight);
    params.xmin = upperLeftLonLat.lon;
    params.xmax = lowerRightLonLat.lon;
    params.ymin = upperLeftLonLat.lat;
    params.ymax = lowerRightLonLat.lat;
} else {
    //三维视图
    params.xmax = Cesium.Math.toDegrees(extend.east);
    params.ymax = Cesium.Math.toDegrees(extend.north);
    params.xmin = Cesium.Math.toDegrees(extend.west);
    params.ymin = Cesium.Math.toDegrees(extend.south);
}
 console.log(`${params.xmin},${params.ymin},${params.xmax},${params.ymax}`, params);

你可能感兴趣的:(Cesium,html5,javascript)