home.page.ts 5.81 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
6
7
import { RestService } from '../rest.service';
import { Observable } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';


Rron Jahja's avatar
Rron Jahja committed
8
declare var H: any;
Rron Jahja's avatar
Rron Jahja committed
9
10
11
12
13
14
@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
Rron Jahja's avatar
Rron Jahja committed
15
  private platform: any;
16
  private map: any;
17
18
19
  bikes = [];
  bikeApi: Observable<any>;

20
  private currentLocation = { lat: 0, lng: 0 };
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  public is3DChecked = false;

  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 }]

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

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


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

40
41
42
43
  constructor(private geolocation: Geolocation,
    public restService: RestService,
    public httpClient: HttpClient) {

Rron Jahja's avatar
Rron Jahja committed
44
45
46
47
    this.platform = new H.service.Platform({
      'apikey': 'tiVTgBnPbgV1spie5U2MSy-obhD9r2sGiOCbBzFY2_k'
    });
  }
48

49
50
51
  ngOnInit() {
    this.getBikesList();
  }
52

53
  ngAfterViewInit() {
Rron Jahja's avatar
Rron Jahja committed
54
    setTimeout(() => {
55
      this.loadmap("2D");
Rron Jahja's avatar
Rron Jahja committed
56
    }, 1000);
57
58
59
60

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

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  getBikesList() {
    this.geolocation.getCurrentPosition(
      {
        maximumAge: 1000, timeout: 5000,
        enableHighAccuracy: true
      }
    ).then((resp) => {
      this.currentLocation.lat = resp.coords.latitude;
      this.currentLocation.lng = resp.coords.longitude;
      let url = 'http://193.196.52.237:8081/admin/bikes' + '?lat=' + this.currentLocation.lat + '&lng=' + this.currentLocation.lng;
      const  headers = new  HttpHeaders().set("Authorization", "Bearer " + this.restService.getToken());
      this.bikeApi = this.httpClient.get(url, {headers});
      this.bikeApi
        .subscribe((resp) => {
          console.log('my data: ', resp);
          this.bikes = resp.data;
        }, (error) => console.log(error));
    }, er => {
      alert('Can not retrieve Location')
    }).catch((error) => {
      alert('Error getting location - ' + JSON.stringify(error))
    });
  }

85
  loadmap(style) {
86
    // Obtain the default map types from the platform object
87
88
89
90
91
92
    var mapStyle = "raster";
    var mapElement = "mapElement2d";
    if (style === "3D") {
      mapStyle = "vector";
      mapElement = "mapElement3d";
    }
93
94
    var defaultLayers = this.platform.createDefaultLayers();
    this.map = new H.Map(
95
96
      this[mapElement].nativeElement,
      defaultLayers[mapStyle].normal.map,
97
      {
98
        zoom: 17,
99
100
101
102
103
        pixelRatio: window.devicePixelRatio || 1
      }
    );
    var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(this.map));
    var ui = H.ui.UI.createDefault(this.map, defaultLayers);
104
105
106
107
108
109
    ui.removeControl("mapsettings");
    // create custom one
    var ms = new H.ui.MapSettingsControl({
      baseLayers: [{
        label: "3D", layer: defaultLayers.vector.normal.map
      }, {
110
        label: "Normal", layer: defaultLayers.raster.normal.map
111
      }, {
112
113
114
        label: "Satellite", layer: defaultLayers.raster.satellite.map
      }, {
        label: "Terrain", layer: defaultLayers.raster.terrain.map
115
116
117
118
119
120
121
122
123
124
125
126
      }
      ],
      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');
127
    var zoom = ui.getControl('zoom');
128

129
130
    mapSettings.setAlignment('top-right');
    zoom.setAlignment('left-top');
131
132
    if (style === "3D") {
      this.map.getViewModel().setLookAtData({ tilt: 60 });
133
    }
134
    this.getLocation(this.map);
135
136
137
    var img = ['../../../assets/images/ic_high.png', '../../../assets/images/ic_medium.png', '../../../assets/images/ic_low.png'];
    for (let i = 0; i < this.locationArr.length; i++) {
      this.addMarker(this.locationArr[i].lat, this.locationArr[i].lng, img[i % 3]);
138
    }
Rron Jahja's avatar
Rron Jahja committed
139
  }
140
141
142
  getCurrentPosition(){
    this.getLocation(this.map);
  }
Rron Jahja's avatar
Rron Jahja committed
143
144
145
146
147
148
149
150
151
  getLocation(map) {
    this.geolocation.getCurrentPosition(
      {
        maximumAge: 1000, timeout: 5000,
        enableHighAccuracy: true
      }
    ).then((resp) => {
      let lat = resp.coords.latitude
      let lng = resp.coords.longitude
152
153
      this.currentLocation.lat = resp.coords.latitude;
      this.currentLocation.lng = resp.coords.longitude;
Rron Jahja's avatar
Rron Jahja committed
154
155
156
157
158
159
160
      this.moveMapToGiven(map, lat, lng);
    }, er => {
      alert('Can not retrieve Location')
    }).catch((error) => {
      alert('Error getting location - ' + JSON.stringify(error))
    });
  }
161

Rron Jahja's avatar
Rron Jahja committed
162
  moveMapToGiven(map, lat, lng) {
163

164
    var icon = new H.map.Icon('../../../assets/images/ic_position.png');
165
166
167
168
169
    // 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
170
171
    map.setCenter({ lat: lat, lng: lng });
  }
172

173
174
175
  expandBikeList() {
    for (let i = 0; i < 20; i++) {
      this.tempArr.push(i + 3);
176
177
178
    }
  }

179
  addMarker(lat, lng, img) {
180
181
182
183
184
185
186
    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);
  }
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

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

  enable3DMaps() {
    this.is3DChecked = true;
    setTimeout(() => {
      this.loadmap("3D");
    }, 100);
  }
Rron Jahja's avatar
Rron Jahja committed
203
}