home.page.ts 8.33 KB
Newer Older
Rron Jahja's avatar
Rron Jahja committed
1
2
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Geolocation } from '@ionic-native/geolocation/ngx';
3
4
5
import { RestService } from '../rest.service';
import { Observable } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
6
import { Storage } from '@ionic/storage';
7
import { ToastService } from '../services/toast.service';
8
9


Rron Jahja's avatar
Rron Jahja committed
10
declare var H: any;
11

Rron Jahja's avatar
Rron Jahja committed
12
13
14
15
16
@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
17

Rron Jahja's avatar
Rron Jahja committed
18
export class HomePage {
Rron Jahja's avatar
Rron Jahja committed
19
  private platform: any;
20
  private map: any;
21
22
23
  bikes = [];
  bikeApi: Observable<any>;

24
  private currentLocation = { lat: 0, lng: 0 };
25

26
  public is3DChecked = false;
27
  public isDetailsVisible = false;
28
  public selectedBike ={id: 0};
29
  public isBikeReserved= false;
30
31
32
33
34
35

  public tempArr = [1, 2];
  public locationArr = [{ lat: 48.778409, lng: 9.179252 },
  { lat: 48.780926, lng: 9.173456 },
  { lat: 48.775174, lng: 9.175459 },
  { lat: 48.793704, lng: 9.191112 }]
36
  public arrayLanLon = { lat: 0, lng: 0 };
37
38
39
40
41
42
43
44
45
  @ViewChild("mapElement2d", { static: false })
  public mapElement2d: ElementRef;

  @ViewChild("mapElement3d", { static: false })
  public mapElement3d: ElementRef;


  //@ViewChild("mapElement", { static: false })
  //public mapElement: ElementRef;
46

47
48
  constructor(private geolocation: Geolocation,
    public restService: RestService,
49
    public httpClient: HttpClient,
50
51
    private storage: Storage,
    private toastService: ToastService) {
52

Rron Jahja's avatar
Rron Jahja committed
53
54
55
56
    this.platform = new H.service.Platform({
      'apikey': 'tiVTgBnPbgV1spie5U2MSy-obhD9r2sGiOCbBzFY2_k'
    });
  }
57

58
59
60
  ngOnInit() {
    this.getBikesList();
  }
61

62
  ngAfterViewInit() {
Rron Jahja's avatar
Rron Jahja committed
63
    setTimeout(() => {
64
      this.loadmap("2D");
Rron Jahja's avatar
Rron Jahja committed
65
    }, 1000);
66
67
68
69

    window.addEventListener('resize', () => this.map.getViewPort().resize());
  }

70
  getBikesList() {
71
72
73
74
    this.geolocation.getCurrentPosition({
      maximumAge: 1000, timeout: 5000,
      enableHighAccuracy: true
    }).then((resp) => {
75
76
      this.currentLocation.lat = resp.coords.latitude;
      this.currentLocation.lng = resp.coords.longitude;
77

78
79
80
81
82
      this.storage.get('token').then((token) => {
        let url = 'http://193.196.52.237:8081/bikes' + '?lat=' + this.currentLocation.lat + '&lng=' + this.currentLocation.lng;
        const headers = new HttpHeaders().set("Authorization", "Bearer " + token);
        this.bikeApi = this.httpClient.get(url, { headers });
        this.bikeApi.subscribe((resp) => {
83
          console.log('my data: ', resp);
84
          this.bikes = resp;
85
        }, (error) => console.log(error));
86
      });
87
88
89
90
91
92
93
    }, er => {
      alert('Can not retrieve Location')
    }).catch((error) => {
      alert('Error getting location - ' + JSON.stringify(error))
    });
  }

94
95
96
97




98
  loadmap(style) {
99
    // Obtain the default map types from the platform object
100
101
102
103
104
105
    var mapStyle = "raster";
    var mapElement = "mapElement2d";
    if (style === "3D") {
      mapStyle = "vector";
      mapElement = "mapElement3d";
    }
106
107
    var defaultLayers = this.platform.createDefaultLayers();
    this.map = new H.Map(
108
109
      this[mapElement].nativeElement,
      defaultLayers[mapStyle].normal.map,
110
      {
111
        zoom: 17,
112
113
114
        pixelRatio: window.devicePixelRatio || 1
      }
    );
115

116
117
    var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(this.map));
    var ui = H.ui.UI.createDefault(this.map, defaultLayers);
118
119
120
121
122
123
    ui.removeControl("mapsettings");
    // create custom one
    var ms = new H.ui.MapSettingsControl({
      baseLayers: [{
        label: "3D", layer: defaultLayers.vector.normal.map
      }, {
124
        label: "Normal", layer: defaultLayers.raster.normal.map
125
      }, {
126
127
128
        label: "Satellite", layer: defaultLayers.raster.satellite.map
      }, {
        label: "Terrain", layer: defaultLayers.raster.terrain.map
129
130
131
132
133
134
135
136
137
138
139
140
      }
      ],
      layers: [{
        label: "layer.traffic", layer: defaultLayers.vector.normal.traffic
      },
      {
        label: "layer.incidents", layer: defaultLayers.vector.normal.trafficincidents
      }
      ]
    });
    ui.addControl("customized", ms);
    var mapSettings = ui.getControl('customized');
141
    var zoom = ui.getControl('zoom');
142

143
144
    mapSettings.setAlignment('top-right');
    zoom.setAlignment('left-top');
145
146
    if (style === "3D") {
      this.map.getViewModel().setLookAtData({ tilt: 60 });
147
    }
148
    this.getLocation(this.map);
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    var img = ['../../../assets/images/100_percent.png', '../../../assets/images/75_percent.png', '../../../assets/images/50_percent.png','../../../assets/images/25_percent.png','../../../assets/images/0_percent.png'];
    for (let i = 0; i < this.bikes.length; i++) {
      if(this.bikes[i].batteryPercentage<100 &&this.bikes[i].batteryPercentage>=75){
        this.addMarker(Number(this.bikes[i].lat), Number(this.bikes[i].lon), img[0]);
      }
      else if(this.bikes[i].batteryPercentage<75 &&this.bikes[i].batteryPercentage>=50){
        this.addMarker(Number(this.bikes[i].lat), Number(this.bikes[i].lon), img[1]);
      }
      else if(this.bikes[i].batteryPercentage<50 &&this.bikes[i].batteryPercentage>=25){
        this.addMarker(Number(this.bikes[i].lat), Number(this.bikes[i].lon), img[2]);
      }else if(this.bikes[i].batteryPercentage<25 &&this.bikes[i].batteryPercentage>=0){
        this.addMarker(Number(this.bikes[i].lat), Number(this.bikes[i].lon), img[3]);
      }
      //console.log("rroni", this.bikes[i].lat);
     // this.addMarker(Number(this.bikes[i].lat), Number(this.bikes[i].lon), img[0]);
      // this.addMarker(Number(48.78077362), 9.17782398, img[i % 3]);
      //alert(this.bikes[i].lat);
166
    }
Rron Jahja's avatar
Rron Jahja committed
167
  }
168
  getCurrentPosition() {
169
170
    this.getLocation(this.map);
  }
Rron Jahja's avatar
Rron Jahja committed
171
172
173
174
175
176
177
178
179
  getLocation(map) {
    this.geolocation.getCurrentPosition(
      {
        maximumAge: 1000, timeout: 5000,
        enableHighAccuracy: true
      }
    ).then((resp) => {
      let lat = resp.coords.latitude
      let lng = resp.coords.longitude
180
181
      this.currentLocation.lat = resp.coords.latitude;
      this.currentLocation.lng = resp.coords.longitude;
Rron Jahja's avatar
Rron Jahja committed
182
183
184
185
186
187
188
      this.moveMapToGiven(map, lat, lng);
    }, er => {
      alert('Can not retrieve Location')
    }).catch((error) => {
      alert('Error getting location - ' + JSON.stringify(error))
    });
  }
189

Rron Jahja's avatar
Rron Jahja committed
190
  moveMapToGiven(map, lat, lng) {
191

192
    var icon = new H.map.Icon('../../../assets/images/current_location.png');
193
194
195
196
197
    // Create a marker using the previously instantiated icon:
    var marker = new H.map.Marker({ lat: lat, lng: lng }, { icon: icon });

    // Add the marker to the map:
    map.addObject(marker);
Rron Jahja's avatar
Rron Jahja committed
198
199
    map.setCenter({ lat: lat, lng: lng });
  }
200

201
202
203
  expandBikeList() {
    for (let i = 0; i < 20; i++) {
      this.tempArr.push(i + 3);
204
205
206
    }
  }

207
  addMarker(lat, lng, img) {
208
209
210
211
212
213
214
    var icon = new H.map.Icon(img);
    // Create a marker using the previously instantiated icon:
    var marker = new H.map.Marker({ lat: lat, lng: lng }, { icon: icon });

    // Add the marker to the map:
    this.map.addObject(marker);
  }
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

  toggle3DMaps() {
    console.log(this.is3DChecked);
    if (!this.is3DChecked) {
      setTimeout(() => {
        this.loadmap("3D");
      }, 1000);
    }
  }

  enable3DMaps() {
    this.is3DChecked = true;
    setTimeout(() => {
      this.loadmap("3D");
    }, 100);
  }
231
232
233
234

  showBikeDetails(bike) {

    this.selectedBike=bike;
235
    this.selectedBike.id=bike.id;
236
237
    this.isDetailsVisible = true;
  }
238
  reserveBike() {
239
240
241
242
243
244
245
    //this.selectedBike=bikeS;
    this.storage.get('token').then((token) => {
      let url = 'http://193.196.52.237:8081/reservation' + '?bikeId=' + this.selectedBike.id;
      const headers = new HttpHeaders().set("Authorization", "Bearer " + token);
      this.bikeApi = this.httpClient.get(url, { headers });
      this.bikeApi.subscribe((resp) => {
        console.log('my data: ', resp);
246
247
248
249
250
251
        this.isBikeReserved=true;
        this.toastService.showToast("Reservation Successful!");
      }, (error) => {
        console.log(error)
        this.toastService.showToast("Only one bike may be reserved or rented at a time")
      });
252
253
254
255
    });
 

  }
256
257
258
259
260
261
262
263
264
265
266
267
  cancelReservation() {
    this.storage.get('token').then((token) => {
      let url = 'http://193.196.52.237:8081/reservation' + '?bikeId=' + this.selectedBike.id;
      const headers = new HttpHeaders().set("Authorization", "Bearer " + token);
      this.bikeApi = this.httpClient.delete(url, { headers });
      this.bikeApi.subscribe((resp) => {
        console.log('my data: ', resp);
        this.isBikeReserved=false;
      }, (error) => console.log(error));
    });

  }
268
  
Rron Jahja's avatar
Rron Jahja committed
269
}