- viewer = new Cesium.Viewer('cesiumContainer');
-
- //操作1
- function oneMouse() {
- camera.setView({
- destination: new Cesium.Cartesian3(x, y, z),
- orientation: {
- heading: headingAngle,
- pitch: pitchAngle,
- roll: rollAngle
- }
- });
- }
- //oneMouse();
-
- //操作2
- function twoMouse() {
- viewer.camera.setView({
- destination: Cesium.Rectangle.fromDegrees(west, south, east, north),
- orientation: {
- heading: headingAngle,
- pitch: pitchAngle,
- roll: rollAngle
- }
- });
- }
- //twoMouse();
-
- //使用键盘,禁用默认控制
- function useKeyboard() {
- // viewer = new Cesium.Viewer('cesiumContainer');
- var scene = viewer.scene;
- var canvas = viewer.canvas;
- canvas.setAttribute('tabindex', '0'); // needed to put focus on the canvas
- canvas.onclick = function () {
- canvas.focus();
- };
- var ellipsoid = viewer.scene.globe.ellipsoid;
-
- //禁用默认控制
- scene.screenSpaceCameraController.enableRotate = false;
- scene.screenSpaceCameraController.enableTranslate = false;
- scene.screenSpaceCameraController.enableZoom = false;
- scene.screenSpaceCameraController.enableTilt = false;
- scene.screenSpaceCameraController.enableLook = false;
-
- //记录当前鼠标位置,标记相机移动轨迹
- var startMousePosition;
- var mousePosition;
- var flags = {
- looking: false,
- moveForward: false,
- moveBackward: false,
- moveUp: false,
- moveDown: false,
- moveLeft: false,
- moveRight: false
- };
-
- //添加事件控制相机的标记
- var handler = new Cesium.ScreenSpaceEventHandler(canvas);
-
- handler.setInputAction(function (movement) {
- flags.looking = true;
- mousePosition = startMousePosition = Cesium.Cartesian3.clone(movement.position);
- }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
-
- handler.setInputAction(function (movement) {
- mousePosition = movement.endPosition;
- }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
-
- handler.setInputAction(function (position) {
- flags.looking = false;
- }, Cesium.ScreenSpaceEventType.LEFT_UP);
-
- //键盘事件
- function getFlagForKeyCode(keyCode) {
- switch (keyCode) {
- case 'W'.charCodeAt(0):
- return 'moveForward';
- case 'S'.charCodeAt(0):
- return 'moveBackward';
- case 'Q'.charCodeAt(0):
- return 'moveUp';
- case 'E'.charCodeAt(0):
- return 'moveDown';
- case 'D'.charCodeAt(0):
- return 'moveRight';
- case 'A'.charCodeAt(0):
- return 'moveLeft';
- default:
- return undefined;
- }
- }
-
- document.addEventListener('keydown', function (e) {
- var flagName = getFlagForKeyCode(e.keyCode);
- if (typeof flagName !== 'undefined') {
- flags[flagName] = true;
- }
- }, false);
-
- document.addEventListener('keyup', function (e) {
- var flagName = getFlagForKeyCode(e.keyCode);
- if (typeof flagName !== 'undefined') {
- flags[flagName] = false;
- }
- }, false);
-
- //更新相机位置
- viewer.clock.onTick.addEventListener(function (clock) {
- var camera = viewer.camera;
-
- //相机指向鼠标指向的方形
- if (flags.looking) {
- var width = canvas.clientWidth;
- var height = canvas.clientHeight;
-
- // Coordinate (0.0, 0.0) will be where the mouse was clicked.
- var x = (mousePosition.x - startMousePosition.x) / width;
- var y = -(mousePosition.y - startMousePosition.y) / height;
-
- var lookFactor = 0.05;
- camera.lookRight(x * lookFactor);
- camera.lookUp(y * lookFactor);
- }
- // 相机移动
- var cameraHeight = ellipsoid.cartesianToCartographic(camera.position).height;
- var moveRate = cameraHeight / 100.0;
-
- if (flags.moveForward) {
- camera.moveForward(moveRate);
- }
- if (flags.moveBackward) {
- camera.moveBackward(moveRate);
- }
- if (flags.moveUp) {
- camera.moveUp(moveRate);
- }
- if (flags.moveDown) {
- camera.moveDown(moveRate);
- }
- if (flags.moveLeft) {
- camera.moveLeft(moveRate);
- }
- if (flags.moveRight) {
- camera.moveRight(moveRate);
- }
-
- //参数限制
- //var west = Cesium.Math.toRadians(-77.0);
- //var south = Cesium.Math.toRadians(38.0);
- //var east = Cesium.Math.toRadians(-72.0);
- //var north = Cesium.Math.toRadians(42.0);
- //var extent = new Cesium.Extent(west, south, east, north);
- //camera.viewExtent(extent, Cesium.Ellipsoid.WGS84);
-
- // find intersection of the pixel picked and an ellipsoid
- var ray = camera.getPickRay(mousePosition);
- var intersection = Cesium.IntersectionTests.rayEllipsoid(ray, Cesium.Ellipsoid.WGS84);
- });
-
-
- }
- useKeyboard();