• mars3D使用需要 import以下文件
  • mars3D获取可视化范围API getExtent()

    1. import * as mars3d from 'mars3d'
    2. import * as Cesium from 'mars3d-cesium'

    查看graphic是否在可视范围内

    1. /* 相机移动结束事件 */
    2. this.map.on(mars3d.EventType.cameraMoveEnd, (event) => {
    3. // 相机移动,获取屏幕范围
    4. this.cameraScreen = this.getViewExtend();
    5. });
    6. // 滚轮变化时, 获取屏幕范围
    7. this.map.on(mars3d.EventType.wheel, (event) => {
    8. this.cameraScreen = this.getViewExtend();
    9. });
    1. // 获取当前车辆的经度和维度,用于判断是否再当前屏幕范围内
    2. let lon = data.lon;
    3. let lat = data.lat;
    4. // 找到了车辆,但没在屏幕范围,就移除车辆
    5. if(lon > this.cameraScreen.maxx || lon < this.cameraScreen.minx || lat > this.cameraScreen.maxy || lat < this.cameraScreen.miny) {
    6. carGraphic.remove();
    7. return;
    8. }

    获取可视化范围

    20210831182214214.png

    1. getViewExtend() {
    2. let params = {};
    3. let extend = viewer.camera.computeViewRectangle();
    4. if (typeof extend === "undefined") {
    5. //2D下会可能拾取不到坐标,extend返回undefined,因此作如下转换
    6. let canvas = viewer.scene.canvas;
    7. let upperLeft = new Cesium.Cartesian2(0, 0);//canvas左上角坐标转2d坐标
    8. let lowerRight = new Cesium.Cartesian2(
    9. canvas.clientWidth,
    10. canvas.clientHeight
    11. );//canvas右下角坐标转2d坐标
    12. let ellipsoid = viewer.scene.globe.ellipsoid;
    13. let upperLeft3 = viewer.camera.pickEllipsoid(
    14. upperLeft,
    15. ellipsoid
    16. );//2D转3D世界坐标
    17. let lowerRight3 = viewer.camera.pickEllipsoid(
    18. lowerRight,
    19. ellipsoid
    20. );//2D转3D世界坐标
    21. let upperLeftCartographic = viewer.scene.globe.ellipsoid.cartesianToCartographic(
    22. upperLeft3
    23. );//3D世界坐标转弧度
    24. let lowerRightCartographic= viewer.scene.globe.ellipsoid.cartesianToCartographic(
    25. lowerRight3
    26. );//3D世界坐标转弧度
    27. let minx = Cesium.Math.toDegrees(upperLeftCartographic.longitude);//弧度转经纬度
    28. let maxx = Cesium.Math.toDegrees(lowerRightCartographic.longitude);//弧度转经纬度
    29. let miny = Cesium.Math.toDegrees(lowerRightCartographic.latitude);//弧度转经纬度
    30. let maxy = Cesium.Math.toDegrees(upperLeftCartographic.latitude);//弧度转经纬度
    31. console.log("经度:" + minx + "----" + maxx);
    32. console.log("纬度:" + miny + "----" + maxy);
    33. params.minx = minx;
    34. params.maxx = maxx;
    35. params.miny = miny;
    36. params.maxy = maxy;
    37. } else {
    38. //3D获取方式
    39. params.maxx = Cesium.Math.toDegrees(extend.east);
    40. params.maxy = Cesium.Math.toDegrees(extend.north);
    41. params.minx = Cesium.Math.toDegrees(extend.west);
    42. params.miny = Cesium.Math.toDegrees(extend.south);
    43. }
    44. return params;//返回屏幕所在经纬度范围
    45. }