Cesium 学习笔记 - cytggit/Map-openlayers GitHub Wiki

  • 去除Cesium的logo和版本信息

      viewer._cesiumWidget._creditContainer.style.display="none";  
    
  • 对entity的操作:添加、隐藏、修改、去除、居中显示

      Var rainEntity=viewer.entities.add({
    
        id: "rain",
    
        name: 'RainStation',
    
        parent: rainLayer3D,                
    
        position: Cesium.Cartesian3.fromDegrees(lon, lat),
    
        billboard: {
    
          image: 'images/pointIcons/rain1.png',
    
          scale:0.7,
    
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM
    
        },
    
        label: {
    
            text: rainfall,
    
            font: '12px SimHei ',
    
            Width: 3,
    
            style: Cesium.LabelStyle.FILL,
    
            fillColor: Cesium.Color.AQUA,
    
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
    
            verticalOrigin: Cesium.VerticalOrigin.TOP
    
        }
    
      });  //添加
    
      viewer.entities.getById("rain").show = false;   //隐藏
    
      viewer.entities.getById("rain").label.text= "drp";   //修改属性
    
      viewer.entities.removeAll();  //移除所有
    
      viewer.zoomTo(rainEntity);   //居中显示
    
  • 去掉entity的双击事件

问题所在:双击entity,会放大视图,entity居中显示,且鼠标左键失去移动功能,鼠标滚轮失去作用

解决问题:

    viewer.screenSpaceEventHandler.setInputAction(function(){},Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK );
  • 获取当前视角高度

      var scene = viewer.scene;
    
      var ellipsoid = scene.globe.ellipsoid;
    
      var height=ellipsoid.cartesianToCartographic(viewer.camera.position).height;
    
  • 获取某个经纬度在屏幕上的位置

      var position = Cesium.Cartesian3.fromDegrees(lon, lat);
    
      var clickPt =Cesium.SceneTransforms.wgs84ToWindowCoordinates (viewer.scene, position);
    
      var screenX=clickPt.x;
    
      var screenY=clickPt.y;
    
  • 获取三维场景屏幕中心点坐标

      var result = viewer.camera.pickEllipsoid(new Cesium.Cartesian2 ( viewer.canvas.clientWidth /2 , viewer.canvas.clientHeight / 2));
    
      var curPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(result);
    
      var lon = curPosition.longitude*180/Math.PI;
    
      var lat = curPosition.latitude*180/Math.PI;
    
  • 响应鼠标单击等事件,获取屏幕点击坐标

      var handler = new Cesium.ScreenSpaceEventHandler(canvas);
    
      handler.setInputAction(function(click){},Cesium.ScreenSpaceEventType.LEFT_CLICK);
    
      var clickX=click.position.x;
    
      var clickY=click.position.y;
    

这个LEFT_CLICK可以换成MIDDLE_CLICK、MOUSE_MOVE等就会响应滚轮点击、鼠标移动等事件,见参考文档中的ScreenSpaceEventType(),注意不同的事件中,function中的click会有不同的属性,可console.log(click),找到所需

  • 跟踪相机视角的改变

      viewer.camera.moveStart.addEventListener(function(moveStartPosition){});
    
      viewer.camera.moveEnd.addEventListener(function(moveEndPosition){});
    

其实还有个

    viewer.camera.changed.addEventListener(function(moveEndPosition){}),但我不会用,总是提示changed不存在,但是camera的参考文档中这个changed和moveStart和moveEnd都可以addEventListener
  • 使视角到达某一地点

      viewer.camera.setView({
    
        destination: Cesium.Cartesian3.fromDegrees(lon, lat, height),
    
        orientation: {
    
          heading : curHeading,  //左右偏移
    
          pitch : curPitch,   //上下偏移
    
          roll : 0.0                           
    
       }
    
      });