Blame view

src/app/component/map/map.component.ts 1.63 KB
287033a62   lsagona   add vessel positi...
1
  import {Component, OnInit} from '@angular/core';
c4e00deb9   lsagona   add map
2
  import * as L from 'leaflet';
287033a62   lsagona   add vessel positi...
3
  import {CircleMarker} from 'leaflet';
c4e00deb9   lsagona   add map
4
5
  import {Vessels} from '../../model/vessels';
  import {VesselsService} from '../../service/vessels.service';
287033a62   lsagona   add vessel positi...
6
  import {Message} from '../../model/message';
c666d6db6   lsagona   add map graph and...
7
8
9
10
11
12
13
  
  @Component({
    selector: 'app-map',
    templateUrl: './map.component.html',
    styleUrls: ['./map.component.scss']
  })
  export class MapComponent implements OnInit {
c4e00deb9   lsagona   add map
14
    vessels: Vessels;
287033a62   lsagona   add vessel positi...
15
    public map: L.Map;
72dfa80e2   lsagona   eache vessel have...
16
    renderer = L.canvas({ padding: 0.5 });
287033a62   lsagona   add vessel positi...
17
    circleMarkers: Map<number, CircleMarker> = new Map<number, CircleMarker>();
c666d6db6   lsagona   add map graph and...
18

287033a62   lsagona   add vessel positi...
19
20
    constructor(private vesselsService: VesselsService) {
    }
c666d6db6   lsagona   add map graph and...
21
22
  
    ngOnInit(): void {
c4e00deb9   lsagona   add map
23
      this.initMap();
287033a62   lsagona   add vessel positi...
24
      this.connectVesselObservable();
c4e00deb9   lsagona   add map
25
26
27
28
29
    }
  
    connectVesselObservable(): void {
      this.vesselsService.currentVessels.subscribe(vessels => {
        this.vessels = vessels;
287033a62   lsagona   add vessel positi...
30
        this.updateMap();
c4e00deb9   lsagona   add map
31
32
33
34
      });
    }
  
    initMap(): void {
72dfa80e2   lsagona   eache vessel have...
35
      this.map = L.map('map').setView([0, 0], 1);
c4e00deb9   lsagona   add map
36
      L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
287033a62   lsagona   add vessel positi...
37
38
39
40
41
42
43
        attribution: 'map',
      }).addTo(this.map);
    }
  
    addCircleMarker(message: Message, color: string): void {
      const circleMarker = L.circleMarker([+message.latitude, +message.longitude],
        {
72dfa80e2   lsagona   eache vessel have...
44
          renderer: this.renderer,
287033a62   lsagona   add vessel positi...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
          radius: 0.1,
          color
        }).addTo(this.map);
      this.circleMarkers.set(Number(message.mmsi), circleMarker);
    }
  
    updateMap(): void {
      let nbMessage = 0;
      this.vessels.vessels.forEach(((vessel, key) => {
        const color = vessel.getColor();
        vessel.messages.forEach((message) => {
          nbMessage++;
          this.addCircleMarker(message, color);
        });
      }));
c666d6db6   lsagona   add map graph and...
60
61
62
    }
  
  }