cesium 获取可视窗口四角坐标

 const getViewExtend = () => {
      let params = {};
      let extend = viewer.camera.computeViewRectangle();
      if (typeof extend === "undefined") {
        //2D下会可能拾取不到坐标,extend返回undefined,因此作如下转换
        let canvas = viewer.scene.canvas;
        let upperLeft = new Cesium.Cartesian2(0, 0); //canvas左上角坐标转2d坐标
        let lowerRight = new Cesium.Cartesian2(
          canvas.clientWidth,
          canvas.clientHeight
        ); //canvas右下角坐标转2d坐标

        let ellipsoid = viewer.scene.globe.ellipsoid;
        let upperLeft3 = viewer.camera.pickEllipsoid(upperLeft, ellipsoid); //2D转3D世界坐标

        let lowerRight3 = viewer.camera.pickEllipsoid(lowerRight, ellipsoid); //2D转3D世界坐标

        let upperLeftCartographic =
          viewer.scene.globe.ellipsoid.cartesianToCartographic(upperLeft3); //3D世界坐标转弧度
        let lowerRightCartographic =
          viewer.scene.globe.ellipsoid.cartesianToCartographic(lowerRight3); //3D世界坐标转弧度

        let minx = Cesium.Math.toDegrees(upperLeftCartographic.longitude); //弧度转经纬度
        let maxx = Cesium.Math.toDegrees(lowerRightCartographic.longitude); //弧度转经纬度

        let miny = Cesium.Math.toDegrees(lowerRightCartographic.latitude); //弧度转经纬度
        let maxy = Cesium.Math.toDegrees(upperLeftCartographic.latitude); //弧度转经纬度

        console.log("经度:" + minx + "----" + maxx);
        console.log("纬度:" + miny + "----" + maxy);

        params.minx = minx;
        params.maxx = maxx;
        params.miny = miny;
        params.maxy = maxy;
      } else {
        //3D获取方式
        params.maxx = Cesium.Math.toDegrees(extend.east);
        params.maxy = Cesium.Math.toDegrees(extend.north);

        params.minx = Cesium.Math.toDegrees(extend.west);
        params.miny = Cesium.Math.toDegrees(extend.south);
      }

      // 返回屏幕所在经纬度范围
      return params;
    };

你可能感兴趣的:(cesium 获取可视窗口四角坐标)