Blame view

src/app/component/map/map.component.ts 2.35 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';
9bbae2409   lsagona   highlight selecte...
7
8
  import {SelectedVesselService} from '../../service/selected-vessel.service';
  import {Vessel} from '../../model/vessel';
c666d6db6   lsagona   add map graph and...
9
10
11
12
13
14
15
  
  @Component({
    selector: 'app-map',
    templateUrl: './map.component.html',
    styleUrls: ['./map.component.scss']
  })
  export class MapComponent implements OnInit {
c4e00deb9   lsagona   add map
16
    vessels: Vessels;
9bbae2409   lsagona   highlight selecte...
17
    selectedVessel: Vessel;
287033a62   lsagona   add vessel positi...
18
    public map: L.Map;
9bbae2409   lsagona   highlight selecte...
19
    renderer = L.canvas({padding: 0.5});
287033a62   lsagona   add vessel positi...
20
    circleMarkers: Map<number, CircleMarker> = new Map<number, CircleMarker>();
c666d6db6   lsagona   add map graph and...
21

9bbae2409   lsagona   highlight selecte...
22
    constructor(private vesselsService: VesselsService, private selectedVesselService: SelectedVesselService) {
287033a62   lsagona   add vessel positi...
23
    }
c666d6db6   lsagona   add map graph and...
24
25
  
    ngOnInit(): void {
c4e00deb9   lsagona   add map
26
      this.initMap();
287033a62   lsagona   add vessel positi...
27
      this.connectVesselObservable();
9bbae2409   lsagona   highlight selecte...
28
      this.connectSelectVesselObservable();
c4e00deb9   lsagona   add map
29
30
31
32
33
    }
  
    connectVesselObservable(): void {
      this.vesselsService.currentVessels.subscribe(vessels => {
        this.vessels = vessels;
287033a62   lsagona   add vessel positi...
34
        this.updateMap();
c4e00deb9   lsagona   add map
35
36
      });
    }
9bbae2409   lsagona   highlight selecte...
37
38
39
40
41
42
    connectSelectVesselObservable(): void {
      this.selectedVesselService.currentVessel.subscribe(vessels => {
        this.selectedVessel = vessels;
        this.updateMap();
      });
    }
c4e00deb9   lsagona   add map
43
    initMap(): void {
72dfa80e2   lsagona   eache vessel have...
44
      this.map = L.map('map').setView([0, 0], 1);
c4e00deb9   lsagona   add map
45
      L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
287033a62   lsagona   add vessel positi...
46
47
48
49
50
        attribution: 'map',
      }).addTo(this.map);
    }
  
    addCircleMarker(message: Message, color: string): void {
9bbae2409   lsagona   highlight selecte...
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
      let circleMarker;
      if (message.mmsi === this.selectedVessel.getMMSI()) {
        circleMarker = L.circleMarker([+message.latitude, +message.longitude],
          {
            renderer: this.renderer,
            radius: 4,
            color: '#ff0000',
          }).addTo(this.map).bringToFront();
      }else {
        circleMarker = L.circleMarker([+message.latitude, +message.longitude],
          {
            renderer: this.renderer,
            radius: 0.01,
            color
          }).addTo(this.map);
      }
287033a62   lsagona   add vessel positi...
67
68
69
70
      this.circleMarkers.set(Number(message.mmsi), circleMarker);
    }
  
    updateMap(): void {
9bbae2409   lsagona   highlight selecte...
71
      this.circleMarkers.clear();
287033a62   lsagona   add vessel positi...
72
73
74
      this.vessels.vessels.forEach(((vessel, key) => {
        const color = vessel.getColor();
        vessel.messages.forEach((message) => {
287033a62   lsagona   add vessel positi...
75
76
77
          this.addCircleMarker(message, color);
        });
      }));
c666d6db6   lsagona   add map graph and...
78
79
80
    }
  
  }