import { HIDE_TRAJECTORY_KEY , ENABLE_SWAP_DIRECTION } from '../../const' ; import { GoogleMap , GoogleMapsEvent , GoogleMapsLatLng } from 'ionic-native' ; import { Platform } from 'ionic-angular' ; import { Component , OnChanges , OnDestroy } from '@angular/core' ; import { Bus } from '../../models/bus' ; import { Line , Itinerary } from '../../models/itinerary' ; import { MarkerController } from './marker' ; import { PreferencesManager } from '../../managers/preferences' ; const mapConfig = { mapType : 'MAP_TYPE_NORMAL' , controls : { compass : true , myLocationButton : true , indoorPicker : false , zoom : false } , camera : { latLng : new GoogleMapsLatLng ( - 22.9083 , - 43.1964 ) , zoom : 12 } , } ; class @ Component ( { selector : 'google-maps' , templateUrl : 'template.html' , inputs : [ 'markers' , 'line' , 'trajectory' ] , } ) export class GoogleMapsComponent implements OnChanges , OnDestroy { private map : GoogleMap ; private mcontrol : MarkerController ; private swap : boolean = false ; private static instance : GoogleMapsComponent ; public markers : Bus [ ] ; public trajectory : Itinerary ; public line : Line ; public swapable : boolean = ENABLE_SWAP_DIRECTION ; public preferences : PreferencesManager ; public constructor ( platform , prefs ) { this . preferences = prefs ; GoogleMapsComponent . instance = this ; platform . ready ( ) . then ( ( ) => this . onPlatformReady ( ) ) ; } private return { void } private onPlatformReady ( ) { this . map = new GoogleMap ( 'map_canvas' ) ; this . mcontrol = new MarkerController ( this . map ) ; this . map . one ( GoogleMapsEvent . MAP_READY ) . then ( ( ) => this . onMapReady ( ) ) ; } return { void } public ngOnDestroy ( ) { this . mcontrol . hideTrajectory ( ) ; this . removeMarkers ( ) ; } param { Object } changes return { void } public ngOnChanges ( changes ) { if ( changes . markers ) this . onMarkerChanges ( changes . markers ) ; if ( changes . trajectory ) this . onTrajectoryChanges ( changes . trajectory ) ; } private return { void } private onMapReady ( ) { console . log ( 'Map ready' ) ; this . map . setOptions ( mapConfig ) ; } return { boolean } public onSwapDirection ( ) { if ( this . swapable ) { let self = GoogleMapsComponent . instance ; if ( self . line . Description !== 'desconhecido' ) { self . swap = ! self . swap ; self . removeMarkers ( ) ; self . updateMarkers ( self . markers ) ; return true ; } } return false ; } private param { Object } trajectory return { void } private onTrajectoryChanges ( trajectory ) { if ( this . trajectory ) { this . mcontrol . hideTrajectory ( ) ; this . preferences . getKey < boolean > ( HIDE_TRAJECTORY_KEY ) . then ( enabled => { if ( ! enabled ) this . mcontrol . showTrajectory ( this . trajectory ) ; } ) ; } } private param { any } value return { boolean } private isArray ( value ) { return value instanceof Array ; } private param { Object } markers return { void } private onMarkerChanges ( markers ) { if ( ! markers . previousValue && this . isArray ( markers . currentValue ) ) { this . removeMarkers ( ) ; this . updateMarkers ( markers . currentValue ) ; } else if ( this . isArray ( markers . previousValue ) && this . isArray ( markers . currentValue ) ) { if ( JSON . stringify ( markers . previousValue ) !== JSON . stringify ( markers . currentValue ) ) this . updateMarkers ( markers . currentValue ) ; } } private param { Bus [ ] } current return { void } private updateMarkers ( current ) { let buses = ( this . swapable ) ? this . filterBuses ( current ) : current ; buses . forEach ( bus => this . mcontrol . setMarker ( bus ) ) ; } private return { void } private removeMarkers ( ) { this . mcontrol . removeMarkers ( ) ; } private param { Bus [ ] } buses return { Bus [ ] } private filterBuses ( buses ) { if ( ! this . swap ) return buses . filter ( bus => bus . Direction === this . line . Description ) ; else return buses . filter ( bus => bus . Direction !== this . line . Description ) ; } }