Advertisement
andersonmfjr

Map.ts - Travlendar

Nov 20th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component } from '@angular/core';
  2. import { IonicPage, ModalController, ViewController, LoadingController, AlertController } from 'ionic-angular';
  3. import { DatePicker } from 'ionic2-date-picker';
  4.  
  5. import { Appointment } from '../../models/appointment';
  6. import { Geolocation } from '@ionic-native/geolocation';
  7. import { User } from '../../models/user';
  8. import { Storage } from '@ionic/storage';
  9. import { Http, Headers } from '@angular/http';
  10.  
  11. declare var google;
  12.  
  13. @IonicPage()
  14. @Component({
  15.   selector: 'page-map',
  16.   templateUrl: 'map.html',
  17.   providers: [DatePicker, Geolocation]
  18. })
  19.  
  20. export class MapPage {
  21.  
  22.   public user = new User('', '', '', '', {});
  23.   public appointments: Appointment[];
  24.   public coords = [];
  25.  
  26.   constructor(public datePicker: DatePicker,
  27.     public modalCtrl: ModalController,
  28.     public viewCtrl: ViewController,
  29.     public geolocation: Geolocation,
  30.     private _loadingCtrl: LoadingController,
  31.     readonly storage: Storage,
  32.     private _http: Http,
  33.     private _alertCtrl: AlertController) {
  34.     this.datePicker = new DatePicker(<any>this.modalCtrl, <any>this.viewCtrl);
  35.     this.datePicker.onDateSelected.subscribe((date) => { console.log(date); });
  36.  
  37.     this.storage.get('user').then((userLoggedIn) => {
  38.       if(userLoggedIn != null){
  39.         this.user = userLoggedIn;
  40.       }
  41.     });
  42.   }
  43.  
  44.   ionViewDidEnter() {
  45.     var headers = new Headers();
  46.     headers.append("Accept", 'application/json');
  47.     headers.append('Content-Type', 'text/plain');
  48.  
  49.     let json = JSON.stringify({ email: '' });
  50.  
  51.     let loader = this._loadingCtrl.create({
  52.       content: 'Loading appointments, please wait ...'
  53.     });
  54.  
  55.     loader.present();
  56.  
  57.     this._http.post('http://ec2-18-220-255-245.us-east-2.compute.amazonaws.com:8080/backend-ttac/getUserAppointments?email=' + this.user.email, json, { headers: headers })
  58.       .subscribe(data => {
  59.  
  60.         loader.dismiss();
  61.         this.appointments = JSON.parse(data['_body']);
  62.         if (this.appointments.length === 0) {
  63.           let alert = this._alertCtrl.create({
  64.             title: 'Attention!!',
  65.             subTitle: 'You have no registered appointments.',
  66.             buttons: ['Ok']
  67.           });
  68.           alert.present();
  69.         }
  70.       }, (err) => {
  71.         loader.dismiss();
  72.         console.log(err);
  73.       });
  74.  
  75.       this.loadMap();
  76.   }
  77.  
  78.   loadMap() {
  79.     this.geolocation.getCurrentPosition().then((position) => {
  80.  
  81.       var map = new google.maps.Map(document.getElementById('map'), {
  82.         zoom: 11,
  83.         center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
  84.         mapTypeId: google.maps.MapTypeId.ROADMAP
  85.       });
  86.  
  87.       for(var i = 0; i < this.appointments.length; i++){
  88.         let address = this.appointments[i].streetName + ' ' + this.appointments[i].number + ' ' + this.appointments[i].city + ' ' + this.appointments[i].state + ' ' + this.appointments[i].postcode;
  89.         this.getCoordinates(address, i, map)
  90.       }
  91.  
  92.       var directionsService = new google.maps.DirectionsService;
  93.       var directionsDisplay = new google.maps.DirectionsRenderer;
  94.  
  95.       directionsDisplay.setMap(map);
  96.     }, (err) => {
  97.       console.log(err);
  98.     });
  99.  
  100.   }
  101.  
  102.   getCoordinates(address, i, map){
  103.     let geocoder = new google.maps.Geocoder();
  104.  
  105.     geocoder.geocode({address: address}, function (results, status){  
  106.       if(status == 'OK'){
  107.         let lat = results[0].geometry.location.lat();
  108.         let lng = results[0].geometry.location.lng();
  109.  
  110.         new google.maps.Marker({
  111.           map: map,
  112.           animation: google.maps.Animation.DROP,
  113.           position: new google.maps.LatLng(lat, lng),
  114.           label: '' + (i + 1) + ''
  115.         });
  116.       }
  117.     })
  118.   }
  119.  
  120.   openCalendar() {
  121.     this.datePicker.showCalendar();
  122.   }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement