Procházet zdrojové kódy

Added reload map function

Jorge Baquero před 7 roky
rodič
revize
0d794147b2
1 změnil soubory, kde provedl 48 přidání a 15 odebrání
  1. 48 15
      src/pages/main/main.ts

+ 48 - 15
src/pages/main/main.ts Zobrazit soubor

@@ -1,6 +1,7 @@
1 1
 import { Component } from '@angular/core';
2 2
 import { NavController } from 'ionic-angular';
3 3
 import { AlertController } from 'ionic-angular';
4
+import { interval } from 'rxjs/observable/interval';
4 5
 
5 6
 import { HomePage } from '../home/home'
6 7
 import { DevicesPage } from '../devices/devices'
@@ -8,6 +9,8 @@ import { ConfigurationPage } from '../configuration/configuration'
8 9
 import { ShareService } from '../../services/share/share';
9 10
 import { VespotServiceProvider } from '../../providers/vespot-service/vespot-service';
10 11
 
12
+
13
+
11 14
 import {
12 15
   GoogleMaps,
13 16
   GoogleMap,
@@ -15,7 +18,8 @@ import {
15 18
   GoogleMapOptions,
16 19
   CameraPosition,
17 20
   MarkerOptions,
18
-  Marker
21
+  Marker,
22
+  LatLng
19 23
 } from '@ionic-native/google-maps';
20 24
 
21 25
 @Component({
@@ -30,6 +34,10 @@ export class Main {
30 34
   rootDevice: any;
31 35
   public rootPosition: any;
32 36
   map: GoogleMap;
37
+  deviceMarker: any;
38
+  newDevice: boolean = true;
39
+
40
+  secondsCounter = interval(30000);
33 41
 
34 42
   constructor(private navCtrl: NavController,
35 43
   public shareService: ShareService,
@@ -41,6 +49,7 @@ export class Main {
41 49
       this.devices = this.shareService.getDevices();
42 50
       this.rootDevice = this.shareService.getRootDevice();
43 51
       this.rootPosition = {};
52
+
44 53
   }
45 54
 
46 55
   ionViewWillEnter() {
@@ -56,16 +65,20 @@ export class Main {
56 65
     this.vespotService.getPositionByDeviceId(auth, this.shareService.getRootDevice().id)
57 66
   	.subscribe(
58 67
         (data) => { // Success
59
-			this.rootPosition = data[0];
68
+
69
+          if(this.newDevice) {
70
+            this.rootPosition = data[0];
60 71
             this.loadMap();
72
+            this.secondsCounter.subscribe((n) => {
73
+              this.updateMarker();
74
+            });
75
+            this.newDevice = false;
76
+        } else {
77
+            this.updateMarker();
78
+        }
61 79
         },
62 80
         (error) =>{
63
-			let alert = this.alertCtrl.create({
64
-              title: 'Error servicio',
65
-              subTitle: error,
66
-              buttons: ['OK']
67
-            });
68
-            alert.present();
81
+			       console.log('getRootDevicePosition: ' + error);
69 82
         }
70 83
       );
71 84
   }
@@ -89,11 +102,10 @@ export class Main {
89 102
     let mapOptions: GoogleMapOptions = {
90 103
       camera: {
91 104
         target: {
92
-          lat: 43.0741904, // default location
93
-          lng: -89.3809802 // default location
105
+          lat: this.rootPosition.latitude, // default location
106
+          lng: this.rootPosition.longitude // default location
94 107
         },
95
-        zoom: 18,
96
-        tilt: 30
108
+        zoom: 18
97 109
       }
98 110
     };
99 111
 
@@ -106,7 +118,7 @@ export class Main {
106 118
       this.getPosition();
107 119
     })
108 120
     .catch(error =>{
109
-      console.log(error);
121
+      console.log('loadMap: ' + error);
110 122
     });
111 123
 
112 124
   }
@@ -118,15 +130,36 @@ export class Main {
118 130
         lng: this.rootPosition.longitude // default location
119 131
       }
120 132
     });
121
-    this.map.addMarker({
133
+    this.deviceMarker = this.map.addMarker({
122 134
       title: this.shareService.getRootDevice().name,
123 135
       icon: 'blue',
124
-      animation: 'DROP',
125 136
       position: {
126 137
         lat: this.rootPosition.latitude, // default location
127 138
         lng: this.rootPosition.longitude // default location
128 139
       }
129 140
     });
141
+    this.deviceMarker.showInfoWindow();
142
+  }
143
+
144
+  updateMarker() {
145
+    this.map.clear().then(() => {
146
+      let auth = this.shareService.getAuthToken();
147
+      this.vespotService.getPositionByDeviceId(auth, this.shareService.getRootDevice().id)
148
+    	.subscribe(
149
+          (data) => { // Success
150
+  			       this.rootPosition = data[0];
151
+               this.getPosition();
152
+          },
153
+          (error) =>{
154
+  			       console.log('updateMarker: ' + error);
155
+          }
156
+        );
157
+    })
158
+    .catch(error =>{
159
+      console.log('loadMap: ' + error);
160
+    });
161
+
162
+
130 163
   }
131 164
 
132 165
 }