File

projects/angular-cesium/src/lib/angular-cesium/services/camera/camera.service.ts

Description

The service exposes the scene's camera and screenSpaceCameraController SceneMode.PERFORMANCE_SCENE2D - is a 3D scene mode that acts like Cesium 2D mode, but is more efficient performance wise.

Index

Properties
Methods

Constructor

constructor()

Methods

_listenToSceneModeMorph
_listenToSceneModeMorph(callback: Function)
Parameters :
Name Type Optional
callback Function No
Returns : void
_revertCameraProperties
_revertCameraProperties()
Returns : void
cameraFlyTo
cameraFlyTo(options: any)
Parameters :
Name Type Optional
options any No
Returns : any
enableInputs
enableInputs(inputs: boolean)

Sets if the camera receives inputs

Parameters :
Name Type Optional
inputs boolean No
Returns : void
enableLook
enableLook(lock: boolean)

Sets if the camera is able to free-look

Parameters :
Name Type Optional
lock boolean No
Returns : void
enableRotate
enableRotate(rotate: boolean)

Sets if the camera is able to rotate

Parameters :
Name Type Optional
rotate boolean No
Returns : void
enableTilt
enableTilt(tilt: boolean)

Sets if the camera is able to tilt

Parameters :
Name Type Optional
tilt boolean No
Returns : void
enableTranslate
enableTranslate(translate: boolean)

Sets if the camera is able to translate

Parameters :
Name Type Optional
translate boolean No
Returns : void
enableZoom
enableZoom(zoom: boolean)

Sets if the camera is able to zoom

Parameters :
Name Type Optional
zoom boolean No
Returns : void
flyTo
flyTo(target: any, options?: any)
Parameters :
Name Type Optional
target any No
options any Yes
Returns : any

Promise

getCamera
getCamera()

Gets the scene's camera

Returns : any
getMaximumZoom
getMaximumZoom()

Gets the maximum zoom value in meters

Returns : number
getMinimumZoom
getMinimumZoom()

Gets the minimum zoom value in meters

Returns : number
getScreenSpaceCameraController
getScreenSpaceCameraController()

Gets the scene's screenSpaceCameraController

Returns : any
init
init(cesiumService: CesiumService)
Parameters :
Name Type Optional
cesiumService CesiumService No
Returns : void
lockRotation
lockRotation(lock: boolean)

Locks or unlocks camera rotation

Parameters :
Name Type Optional
lock boolean No
Returns : void
setMaximumZoom
setMaximumZoom(amount: number)

Sets the maximum zoom value in meters

Parameters :
Name Type Optional
amount number No
Returns : void
setMinimumZoom
setMinimumZoom(amount: number)

Sets the minimum zoom value in meters

Parameters :
Name Type Optional
amount number No
Returns : void
setRotation
setRotation(degreesInRadians: number)

Set camera's rotation

Parameters :
Name Type Optional
degreesInRadians number No
Returns : void
setSceneMode
setSceneMode(sceneMode: SceneMode, duration?: number)

Sets the map's SceneMode

Parameters :
Name Type Optional Description
sceneMode SceneMode No
  • The SceneMode to morph the scene into.
duration number Yes
  • The duration of scene morph animations, in seconds
Returns : void
setView
setView(options: any)
Parameters :
Name Type Optional Description
options any No

viewer options

Returns : void
trackEntity
trackEntity(cesiumEntity?: any, options?: literal type)
Parameters :
Name Type Optional Description
cesiumEntity any Yes
  • cesium entity( billboard, polygon...) to track
options literal type Yes
  • track entity options
Returns : any
untrackEntity
untrackEntity()
Returns : void
zoomIn
zoomIn(amount: number)

Zooms amount along the camera's view vector. API: https://cesiumjs.org/Cesium/Build/Documentation/Camera.html#zoomIn

Parameters :
Name Type Optional
amount number No
Returns : any
zoomOut
zoomOut(amount: number)

Zooms amount along the opposite direction of the camera's view vector. API: https://cesiumjs.org/Cesium/Build/Documentation/Camera.html#zoomOut

Parameters :
Name Type Optional
amount number No
Returns : any
zoomTo
zoomTo(target: any, offset?: any)
Parameters :
Name Type Optional
target any No
offset any Yes
Returns : any

Promise

Properties

Private camera
camera: any
Type : any
Private isSceneModePerformance2D
isSceneModePerformance2D:
Default value : false
Private lastLook
lastLook: boolean
Type : boolean
Private lastRotate
lastRotate: boolean
Type : boolean
Private lastTilt
lastTilt: boolean
Type : boolean
Private morphListenerCancelFn
morphListenerCancelFn: any
Type : any
Static PERFORMANCE_2D_ALTITUDE
PERFORMANCE_2D_ALTITUDE: number
Type : number
Default value : 25000000
Private scene
scene: any
Type : any
Private screenSpaceCameraController
screenSpaceCameraController: any
Type : any
Private viewer
viewer: any
Type : any
import { Injectable } from '@angular/core';
import { CesiumService } from '../cesium/cesium.service';
import { SceneMode } from '../../models/scene-mode.enum';

/**
 *  The service exposes the scene's camera and screenSpaceCameraController
 *  SceneMode.PERFORMANCE_SCENE2D -  is a 3D scene mode that acts like Cesium 2D mode,
 *  but is more efficient performance wise.
 */
@Injectable()
export class CameraService {
  static PERFORMANCE_2D_ALTITUDE = 25000000;

  private viewer: any;
  private scene: any;
  private camera: any;
  private screenSpaceCameraController: any;
  private morphListenerCancelFn: any;
  private lastRotate: boolean;
  private lastTilt: boolean;
  private lastLook: boolean;
  private isSceneModePerformance2D = false;

  constructor() {
  }

  init(cesiumService: CesiumService) {
    this.viewer = cesiumService.getViewer();
    this.scene = cesiumService.getScene();
    this.screenSpaceCameraController = this.scene.screenSpaceCameraController;
    this.camera = this.scene.camera;
    this.lastRotate = this.screenSpaceCameraController.enableRotate;
    this.lastTilt = this.screenSpaceCameraController.enableTilt;
    this.lastLook = this.screenSpaceCameraController.enableLook;
  }

  _listenToSceneModeMorph(callback: Function) {
    this.morphListenerCancelFn = this.scene.morphStart.addEventListener(
      callback
    );
  }

  _revertCameraProperties() {
    this.isSceneModePerformance2D = false;
    this.enableTilt(this.lastTilt);
    this.enableRotate(this.lastRotate);
    this.enableLook(this.lastLook);
  }

  /**
   * Gets the scene's camera
   */
  getCamera() {
    return this.camera;
  }

  /**
   * Gets the scene's screenSpaceCameraController
   */
  getScreenSpaceCameraController() {
    return this.screenSpaceCameraController;
  }

  /**
   * Gets the minimum zoom value in meters
   */
  getMinimumZoom(): number {
    return this.screenSpaceCameraController.minimumZoomDistance;
  }

  /**
   * Sets the minimum zoom value in meters
   * @param zoom amount
   */
  setMinimumZoom(amount: number): void {
    this.screenSpaceCameraController.minimumZoomDistance = amount;
  }

  /**
   * Gets the maximum zoom value in meters
   */
  getMaximumZoom(): number {
    return this.screenSpaceCameraController.maximumZoomDistance;
  }

  /**
   * Sets the maximum zoom value in meters
   * @param zoom amount
   */
  setMaximumZoom(amount: number): void {
    this.screenSpaceCameraController.maximumZoomDistance = amount;
  }

  /**
   * Sets if the camera is able to tilt
   */
  enableTilt(tilt: boolean): void {
    this.screenSpaceCameraController.enableTilt = tilt;
  }

  /**
   * Sets if the camera is able to rotate
   */
  enableRotate(rotate: boolean): void {
    this.screenSpaceCameraController.enableRotate = rotate;
  }

  /**
   * Sets if the camera is able to free-look
   */
  enableLook(lock: boolean): void {
    this.screenSpaceCameraController.enableLook = lock;
  }

  /**
   * Sets if the camera is able to translate
   */
  enableTranslate(translate: boolean): void {
    this.screenSpaceCameraController.enableTranslate = translate;
  }

  /**
   * Sets if the camera is able to zoom
   */
  enableZoom(zoom: boolean): void {
    this.screenSpaceCameraController.enableZoom = zoom;
  }

  /**
   * Sets if the camera receives inputs
   */
  enableInputs(inputs: boolean): void {
    this.screenSpaceCameraController.enableInputs = inputs;
  }

  /**
   * Sets the map's SceneMode
   * @param sceneMode - The SceneMode to morph the scene into.
   * @param duration - The duration of scene morph animations, in seconds
   */
  setSceneMode(sceneMode: SceneMode, duration?: number) {
    switch (sceneMode) {
      case SceneMode.SCENE3D: {
        if (this.isSceneModePerformance2D) {
          this._revertCameraProperties();
        }

        this.scene.morphTo3D(duration);

        break;
      }
      case SceneMode.COLUMBUS_VIEW: {
        if (this.isSceneModePerformance2D) {
          this._revertCameraProperties();
        }

        this.scene.morphToColumbusView(duration);

        break;
      }
      case SceneMode.SCENE2D: {
        if (this.isSceneModePerformance2D) {
          this._revertCameraProperties();
        }
        this.scene.morphTo2D(duration);

        break;
      }
      case SceneMode.PERFORMANCE_SCENE2D: {
        this.isSceneModePerformance2D = true;
        this.lastLook = this.screenSpaceCameraController.enableLook;
        this.lastTilt = this.screenSpaceCameraController.enableTilt;
        this.lastRotate = this.screenSpaceCameraController.enableRotate;
        this.screenSpaceCameraController.enableTilt = false;
        this.screenSpaceCameraController.enableRotate = false;
        this.screenSpaceCameraController.enableLook = false;
        if (this.morphListenerCancelFn) {
          this.morphListenerCancelFn();
        }
        this.scene.morphToColumbusView(duration);
        const morphCompleteEventListener = this.scene.morphComplete.addEventListener(
          () => {
            this.camera.setView({
              destination: Cesium.Cartesian3.fromDegrees(
                0.0,
                0.0,
                Math.min(
                  CameraService.PERFORMANCE_2D_ALTITUDE,
                  this.getMaximumZoom()
                )
              ),
              orientation: {
                pitch: Cesium.Math.toRadians(-90)
              }
            });
            morphCompleteEventListener();
            this._listenToSceneModeMorph(
              this._revertCameraProperties.bind(this)
            );
          }
        );

        break;
      }
    }
  }

  /**
   * Flies the camera to a destination
   * API: https://cesiumjs.org/Cesium/Build/Documentation/Camera.html?classFilter=cam#flyTo
   */
  cameraFlyTo(options: any) {
    return this.camera.flyTo(options);
  }

  /**
   * Flies the camera to a target
   * API: https://cesiumjs.org/Cesium/Build/Documentation/Viewer.html?classFilter=viewer#flyTo
   * @returns Promise<boolean>
   */
  flyTo(target: any, options?: any) {
    return this.viewer.flyTo(target, options);
  }

  /**
   * Zooms amount along the camera's view vector.
   * API: https://cesiumjs.org/Cesium/Build/Documentation/Camera.html#zoomIn
   */
  zoomIn(amount: number) {
    return this.camera.zoomIn(amount || this.camera.defaultZoomAmount);
  }

  /**
   * Zooms amount along the opposite direction of the camera's view vector.
   * API: https://cesiumjs.org/Cesium/Build/Documentation/Camera.html#zoomOut
   */
  zoomOut(amount: number) {
    return this.camera.zoomOut(amount || this.camera.defaultZoomAmount);
  }

  /**
   * Zoom the camera to a target
   * API: https://cesiumjs.org/Cesium/Build/Documentation/Viewer.html?classFilter=viewer#zoomTo
   * @returns Promise<boolean>
   */
  zoomTo(target: any, offset?: any) {
    return this.viewer.zoomTo(target, offset);
  }

  /**
   * Flies the camera to a destination
   * API: https://cesiumjs.org/Cesium/Build/Documentation/Camera.html?classFilter=camera#setView
   * @param options viewer options
   */
  setView(options: any) {
    this.camera.setView(options);
  }

  /**
   * Set camera's rotation
   */
  setRotation(degreesInRadians: number) {
    this.setView({orientation: {heading: degreesInRadians}});
  }

  /**
   * Locks or unlocks camera rotation
   */
  lockRotation(lock: boolean) {
    this.scene.screenSpaceCameraController.enableRotate = !lock;
  }

  /**
   * Make the camera track a specific entity
   * API: https://cesiumjs.org/Cesium/Build/Documentation/Viewer.html?classFilter=viewer#trackedEntity
   * @param cesiumEntity - cesium entity( billboard, polygon...) to track
   * @param options - track entity options
   */
  trackEntity(
    cesiumEntity?: any,
    options?: { flyTo: boolean; flyToDuration?: number; altitude?: number }
  ) {
    const flyTo = (options && options.flyTo) || false;

    this.viewer.trackedEntity = undefined;
    return new Promise(resolve => {
      if (flyTo) {
        const flyToDuration = (options && options.flyToDuration) || 1;
        const altitude = (options && options.altitude) || 10000;

        // Calc entity flyTo position and wanted altitude
        const entPosCar3 = cesiumEntity.position.getValue(Cesium.JulianDate.now());
        const entPosCart = Cesium.Cartographic.fromCartesian(entPosCar3);
        const zoomAmount = altitude - entPosCart.height;
        entPosCart.height = altitude;
        const flyToPosition = Cesium.Cartesian3.fromRadians(
          entPosCart.longitude,
          entPosCart.latitude,
          entPosCart.height
        );

        this.cameraFlyTo({
          duration: flyToDuration,
          destination: flyToPosition,
          complete: () => {
            this.viewer.trackedEntity = cesiumEntity;
            setTimeout(() => {
              if (zoomAmount > 0) {
                this.camera.zoomOut(zoomAmount);
              } else {
                this.camera.zoomIn(zoomAmount);
              }
            }, 0);
            resolve();
          }
        });
      } else {
        this.viewer.trackedEntity = cesiumEntity;
        resolve();
      }
    });
  }

  untrackEntity() {
    this.trackEntity();
  }
}

result-matching ""

    No results matching ""