home.page.ts 3.06 KB
Newer Older
Rron Jahja's avatar
Rron Jahja committed
1
2
3
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Geolocation } from '@ionic-native/geolocation/ngx';
declare var H: any;
Rron Jahja's avatar
Rron Jahja committed
4
5
6
7
8
9
@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
Rron Jahja's avatar
Rron Jahja committed
10
  private platform: any;
11
12
13
  private map: any;
  private currentLocation = {lat:0,lng:0};

14
15
16
17
18
19
  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}]

Rron Jahja's avatar
Rron Jahja committed
20
21
22
23
24
25
26
  @ViewChild("map", { static: false })
  public mapElement: ElementRef;
  public constructor(private geolocation: Geolocation) {
    this.platform = new H.service.Platform({
      'apikey': 'tiVTgBnPbgV1spie5U2MSy-obhD9r2sGiOCbBzFY2_k'
    });
  }
27

Rron Jahja's avatar
Rron Jahja committed
28
  public ngOnInit() { }
29

Rron Jahja's avatar
Rron Jahja committed
30
31
32
33
  public ngAfterViewInit() {
    setTimeout(() => {
      this.loadmap();
    }, 1000);
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

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

  loadmap() {
    // Obtain the default map types from the platform object
    var defaultLayers = this.platform.createDefaultLayers();
    this.map = new H.Map(
      this.mapElement.nativeElement,
      defaultLayers.vector.normal.map,
      {
        zoom: 16,
        center: { lat: 40.757601, lng: -73.985328 },
        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);
    this.getLocation(this.map);
53
54
    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++){
55
      this.addMarker(this.locationArr[i].lat, this.locationArr[i].lng, img[i%3]);
56
    }
Rron Jahja's avatar
Rron Jahja committed
57
  }
Rron Jahja's avatar
Rron Jahja committed
58

Rron Jahja's avatar
Rron Jahja committed
59
60
61
62
63
64
65
66
67
  getLocation(map) {
    this.geolocation.getCurrentPosition(
      {
        maximumAge: 1000, timeout: 5000,
        enableHighAccuracy: true
      }
    ).then((resp) => {
      let lat = resp.coords.latitude
      let lng = resp.coords.longitude
68
69
      this.currentLocation.lat = resp.coords.latitude;
      this.currentLocation.lng = resp.coords.longitude;
Rron Jahja's avatar
Rron Jahja committed
70
71
72
73
74
75
76
      this.moveMapToGiven(map, lat, lng);
    }, er => {
      alert('Can not retrieve Location')
    }).catch((error) => {
      alert('Error getting location - ' + JSON.stringify(error))
    });
  }
77

Rron Jahja's avatar
Rron Jahja committed
78
  moveMapToGiven(map, lat, lng) {
79
80
81
82
83
84
85
    
    var icon = new H.map.Icon('../../../assets/images/icon_map_currentLocation.png');
    // 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
86
87
    map.setCenter({ lat: lat, lng: lng });
  }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

  expandBikeList(){
    for(let i=0; i<20; i++) {
      this.tempArr.push(i+3);
    }
  }

  addMarker(lat, lng, img){
    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);
  }
Rron Jahja's avatar
Rron Jahja committed
103
}