author Damien Dell'Amico <damien.dellamico@gmail.com>
 *  copyright license import { Component } from '@angular/core' ; import { MapComponent } from '../../components/map/map' ; import { Vibration } from 'ionic-native' ; import { MapService } from '../../providers/map/map.service' ; import { LoadingController , AlertController , ModalController , NavController , Platform } from 'ionic-angular' ; import { GeocoderService } from '../../providers/map/geocoder.service' ; import { SearchPage } from '../search/search' ; import { ConfirmationPage } from '../confirmation/confirmation' ; import { BasePage } from '../base-page' ; @ Component ( { templateUrl : 'home.tpl.html' } ) export class HomePage extends BasePage { localized : boolean = false ; constructor ( private platform , private nav , private geocoderService , private mapService , private modalCtrl , private loadingCtrl , protected alertCtrl ) { super ( alertCtrl ) ; } /***
     * This event is fired when map has fully loaded
     */ onMapReady ( ) { return this . platform . ready ( ) . then ( ( ) => { return this . locate ( ) . then ( ( ) => { const mapElement = this . mapService . mapElement ; if ( mapElement ) { mapElement . classList . add ( 'show-map' ) ; this . mapService . resizeMap ( ) ; } } ) ; } ) ; } /***
     * This event is fired when the map becomes idle after panning or zooming.
     */ onMapIdle ( ) { if ( ! this . localized ) return ; const position = this . mapService . mapCenter ; this . geocoderService . addressForlatLng ( position . lat ( ) , position . lng ( ) ) . subscribe ( ( address ) => { const content = `<div padding><strong>${ address }</strong></div>` ; this . mapService . createInfoWindow ( content , position ) ; } , ( error ) => { this . displayErrorAlert ( ) ; console . error ( error ) ; } ) ; } /***
     * This event is fired when the user starts dragging the map.
     */ onDragStart ( ) { this . mapService . closeInfoWindow ( ) ; } openModal ( ) { const searchModal = this . modalCtrl . create ( SearchPage ) ; searchModal . present ( ) ; } goToConfirmation ( ) { this . nav . push ( ConfirmationPage ) ; } /**
     * Get the current position
     */ private locate ( ) { const loader = this . loadingCtrl . create ( { content : 'Please wait...' , } ) ; loader . present ( ) ; return this . mapService . setPosition ( ) . then ( ( ) => { this . localized = true ; Vibration . vibrate ( 1000 ) ; } ) . catch ( error => { this . alertNoGps ( ) ; console . warn ( error ) ; } ) . then ( ( ) => { setTimeout ( ( ) => { loader . dismiss ( ) ; } , 1000 ) ; } ) ; } private alertNoGps ( ) { const alert = this . alertCtrl . create ( { title : 'Ionic Taxi' , subTitle : 'Gps and network locations are unavailable. Click OK to retry' , enableBackdropDismiss : false , buttons : [ { text : 'OK' , handler : ( ) => { setTimeout ( ( ) => this . locate ( ) , 1500 ) ; } } ] , } ) ; alert . present ( ) ; } }