import { AfterViewInit , Component , ElementRef , NgZone , OnDestroy , ViewChild , } from '@angular/core' ; import { Subscription } from 'rxjs' ; import { transform } from 'ol/proj' ; import { HS_PRMS } from '../permalink/get-params' ; import { HsConfig } from '../../config.service' ; import { HsCoreService } from '../core/core.service' ; import { HsEventBusService } from '../core/event-bus.service' ; import { HsMapService } from './map.service' ; import { HsShareUrlService } from '../permalink/share-url.service' ; @ Component ( { selector : 'hs-map' , templateUrl : './partials/map.html' , } ) export class HsMapComponent implements AfterViewInit , OnDestroy { @ ViewChild ( 'map' ) map : ElementRef ; unregisterMapSyncCenterHandlerSubscription : Subscription ; constructor ( public HsMapService , public HsPermalinkUrlService , public HsCoreService , public HsConfig , public HsEventBusService , private zone ) { this . unregisterMapSyncCenterHandlerSubscription = this . HsEventBusService . mapCenterSynchronizations . subscribe ( ( data ) => { this . onCenterSync ( data ) ; } ) ; } ngAfterViewInit ( ) { this . HsMapService . mapElement = this . map . nativeElement ; const visibleLayersParam = this . HsPermalinkUrlService . getParamValue ( HS_PRMS . visibleLayers ) ; if ( visibleLayersParam ) { this . HsMapService . visibleLayersInUrl = visibleLayersParam . split ( ';' ) ; } this . zone . runOutsideAngular ( ( ) => this . HsMapService . init ( ) ) ; const pos = this . HsPermalinkUrlService . getParamValues ( [ HS_PRMS . x , HS_PRMS . y , HS_PRMS . zoom , ] ) ; if ( ! Object . keys ( pos ) . some ( ( k ) => pos [ k ] == undefined || pos [ k ] == 'NaN' ) ) { this . HsMapService . moveToAndZoom ( parseFloat ( pos [ HS_PRMS . x ] ) , parseFloat ( pos [ HS_PRMS . y ] ) , parseInt ( pos [ HS_PRMS . zoom ] ) ) ; } if ( this . HsPermalinkUrlService . getParamValue ( HS_PRMS . pureMap ) || this . HsConfig . pureMap == true ) { this . HsCoreService . puremapApp = true ; } this . HsMapService . map . updateSize ( ) ; } ngOnDestroy ( ) { this . unregisterMapSyncCenterHandlerSubscription . unsubscribe ( ) ; } param data onCenterSync ( data ? ) { const center = data . center ; if ( ! center ) { return ; } const toProj = this . HsMapService . getCurrentProj ( ) ; const transformed = transform ( [ center [ 0 ] , center [ 1 ] ] , 'EPSG:4326' , toProj ) ; this . HsMapService . moveToAndZoom ( transformed [ 0 ] , transformed [ 1 ] , this . zoomForResolution ( center [ 2 ] ) ) ; } param resolution returns zoomForResolution ( resolution ) { let zoom = 0 ; resolution = Math . abs ( resolution ) ; let r = 156543.03390625 ; while ( resolution < r ) { r /= 2.0 ; zoom ++ ; if ( resolution > r ) { return zoom ; } } return zoom ; } }