home.page.ts 4.52 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
  private map: any;
12
  private currentLocation = { lat: 0, lng: 0 };
13

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  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;
31

Rron Jahja's avatar
Rron Jahja committed
32
33
34
35
36
  public constructor(private geolocation: Geolocation) {
    this.platform = new H.service.Platform({
      'apikey': 'tiVTgBnPbgV1spie5U2MSy-obhD9r2sGiOCbBzFY2_k'
    });
  }
37

Rron Jahja's avatar
Rron Jahja committed
38
  public ngOnInit() { }
39

Rron Jahja's avatar
Rron Jahja committed
40
41
  public ngAfterViewInit() {
    setTimeout(() => {
42
      this.loadmap("2D");
Rron Jahja's avatar
Rron Jahja committed
43
    }, 1000);
44
45
46
47

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

48
  loadmap(style) {
49
    // Obtain the default map types from the platform object
50
51
52
53
54
55
    var mapStyle = "raster";
    var mapElement = "mapElement2d";
    if (style === "3D") {
      mapStyle = "vector";
      mapElement = "mapElement3d";
    }
56
57
    var defaultLayers = this.platform.createDefaultLayers();
    this.map = new H.Map(
58
59
      this[mapElement].nativeElement,
      defaultLayers[mapStyle].normal.map,
60
      {
61
        zoom: 17,
62
63
64
65
66
        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);
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    ui.removeControl("mapsettings");
    // create custom one
    var ms = new H.ui.MapSettingsControl({
      baseLayers: [{
        label: "3D", layer: defaultLayers.vector.normal.map
      },{
        label: "normal", layer: defaultLayers.raster.normal.map
      }, {
        label: "satellite", layer: defaultLayers.raster.satellite.map
      }, {
        label: "terrain", layer: defaultLayers.raster.terrain.map
      }
      ],
      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');
var zoom = ui.getControl('zoom');

mapSettings.setAlignment('top-right');
zoom.setAlignment('top-left');
94
    this.getLocation(this.map);
95
96
97
    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]);
98
    }
Rron Jahja's avatar
Rron Jahja committed
99
  }
Rron Jahja's avatar
Rron Jahja committed
100

Rron Jahja's avatar
Rron Jahja committed
101
102
103
104
105
106
107
108
109
  getLocation(map) {
    this.geolocation.getCurrentPosition(
      {
        maximumAge: 1000, timeout: 5000,
        enableHighAccuracy: true
      }
    ).then((resp) => {
      let lat = resp.coords.latitude
      let lng = resp.coords.longitude
110
111
      this.currentLocation.lat = resp.coords.latitude;
      this.currentLocation.lng = resp.coords.longitude;
Rron Jahja's avatar
Rron Jahja committed
112
113
114
115
116
117
118
      this.moveMapToGiven(map, lat, lng);
    }, er => {
      alert('Can not retrieve Location')
    }).catch((error) => {
      alert('Error getting location - ' + JSON.stringify(error))
    });
  }
119

Rron Jahja's avatar
Rron Jahja committed
120
  moveMapToGiven(map, lat, lng) {
121

122
123
124
125
126
127
    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
128
129
    map.setCenter({ lat: lat, lng: lng });
  }
130

131
132
133
  expandBikeList() {
    for (let i = 0; i < 20; i++) {
      this.tempArr.push(i + 3);
134
135
136
    }
  }

137
  addMarker(lat, lng, img) {
138
139
140
141
142
143
144
    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);
  }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

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