File

projects/angular-cesium/src/lib/angular-cesium/services/maps-manager/maps-manager.service.ts

Description

The service manages ac-map instances. ac-map register itself to this service. This allows retrieval of maps provided services outside of ac-map scope.

Index

Properties
Methods

Constructor

constructor()

Methods

_registerMap
_registerMap(id: string, acMap: AcMapComponent)
Parameters :
Name Type Optional
id string No
acMap AcMapComponent No
Returns : string
_removeMapById
_removeMapById(id: string)
Parameters :
Name Type Optional
id string No
Returns : any
Private generateDefaultId
generateDefaultId()
Returns : string
getMap
getMap(id?: string)
Parameters :
Name Type Optional
id string Yes
sync2DMapsCameras
sync2DMapsCameras(mapsConfiguration: literal type[])

Binds multiple 2D map's cameras together.

Parameters :
Name Type Optional Description
mapsConfiguration literal type[] No
  • binding options. mapId - the id of the maps to bind. sensitivity - the amount the camera position should change in order to sync other maps. bindZoom - should bind zoom level
Returns : void
unsyncMapsCameras
unsyncMapsCameras()

Unsyncs maps cameras

Returns : void

Properties

Private _Maps
_Maps:
Default value : new Map<string, AcMapComponent>()
Private defaultIdCounter
defaultIdCounter: number
Type : number
Default value : 0
Private eventRemoveCallbacks
eventRemoveCallbacks: Function[]
Type : Function[]
Default value : []
Private firstMap
firstMap: any
Type : any
import { Injectable } from '@angular/core';
import { AcMapComponent } from '../../components/ac-map/ac-map.component';

/**
 *  The service manages `ac-map` instances. `ac-map` register itself to this service.
 *  This allows retrieval of maps provided services outside of `ac-map` scope.
 */
@Injectable()
export class MapsManagerService {
  private defaultIdCounter = 0;
  private _Maps = new Map<string, AcMapComponent>();
  private firstMap: any;
  private eventRemoveCallbacks: Function[] = [];

  constructor() {
  }

  getMap(id?: string): AcMapComponent | undefined {
    if (!id) {
      return this.firstMap;
    }
    return this._Maps.get(id);
  }

  _registerMap(id: string, acMap: AcMapComponent): string {
    if (!this.firstMap) {
      this.firstMap = acMap;
    }

    const mapId = id ? id : this.generateDefaultId();
    if (this._Maps.has(mapId)) {
      throw new Error(`Map with id: ${mapId} already exist`);
    }
    this._Maps.set(mapId, acMap);
    return mapId;
  }

  _removeMapById(id: string) {
    return this._Maps.delete(id);
  }

  private generateDefaultId(): string {
    this.defaultIdCounter++;
    return 'default-map-id-' + this.defaultIdCounter;
  }

  /**
   * Binds multiple 2D map's cameras together.
   * @param mapsConfiguration - binding options.
   * mapId - the id of the maps to bind.
   * sensitivity - the amount the camera position should change in order to sync other maps.
   * bindZoom - should bind zoom level
   */
  sync2DMapsCameras(mapsConfiguration: { id: string; sensitivity?: number; bindZoom?: boolean }[]) {
    const DEFAULT_SENSITIVITY = 0.01;
    this.unsyncMapsCameras();
    const maps: { map: AcMapComponent; options?: { sensitivity?: number; bindZoom?: boolean } }[] = mapsConfiguration.map(config => {
      const map = this.getMap(config.id);
      if (!map) {
        throw new Error(`Couldn't find map with id: ${config.id}`);
      }

      return {map, options: {sensitivity: config.sensitivity, bindZoom: config.bindZoom}};
    });

    maps.forEach(masterMapConfig => {
      const masterMap = masterMapConfig.map;
      const options = masterMapConfig.options;
      const masterCamera = masterMap.getCameraService().getCamera();
      const masterCameraCartographic = masterCamera.positionCartographic;
      masterCamera.percentageChanged = options.sensitivity || DEFAULT_SENSITIVITY;
      const removeCallback = masterCamera.changed.addEventListener(() => {
        maps.forEach(slaveMapConfig => {
          const slaveMap = slaveMapConfig.map;
          const slaveMapOptions = slaveMapConfig.options;
          if (slaveMap === masterMap) {
            return;
          }

          const slaveCamera = slaveMap.getCameraService().getCamera();
          const slaveCameraCartographic = slaveCamera.positionCartographic;
          const position = Cesium.Ellipsoid.WGS84.cartographicToCartesian({
            longitude: masterCameraCartographic.longitude,
            latitude: masterCameraCartographic.latitude,
            height: slaveMapOptions.bindZoom ? masterCameraCartographic.height : slaveCameraCartographic.height,
          });

          if (slaveMap.getCesiumViewer().scene.mode !== Cesium.SceneMode.MORPHING) {
            slaveCamera.setView({
              destination: position,
              orientation: {
                heading: slaveCamera.heading,
                pitch: slaveCamera.pitch,
              },
            });
          }
        });
      });
      this.eventRemoveCallbacks.push(removeCallback);
    });
  }

  /**
   * Unsyncs maps cameras
   */
  unsyncMapsCameras() {
    this.eventRemoveCallbacks.forEach(removeCallback => removeCallback());
    this.eventRemoveCallbacks = [];
  }
}

result-matching ""

    No results matching ""