Blame view

src/app/component/map/map.component.ts 3.27 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});
a77940a0e   lsagona   fix a bug when yo...
20
21
    circleMarkers: Map<number, Array<CircleMarker>> = new Map<number, Array<CircleMarker>>();
    highlightedMarkerId: number;
c666d6db6   lsagona   add map graph and...
22

9bbae2409   lsagona   highlight selecte...
23
    constructor(private vesselsService: VesselsService, private selectedVesselService: SelectedVesselService) {
287033a62   lsagona   add vessel positi...
24
    }
c666d6db6   lsagona   add map graph and...
25
26
  
    ngOnInit(): void {
c4e00deb9   lsagona   add map
27
      this.initMap();
287033a62   lsagona   add vessel positi...
28
      this.connectVesselObservable();
9bbae2409   lsagona   highlight selecte...
29
      this.connectSelectVesselObservable();
c4e00deb9   lsagona   add map
30
31
32
33
34
    }
  
    connectVesselObservable(): void {
      this.vesselsService.currentVessels.subscribe(vessels => {
        this.vessels = vessels;
287033a62   lsagona   add vessel positi...
35
        this.updateMap();
c4e00deb9   lsagona   add map
36
37
      });
    }
9bbae2409   lsagona   highlight selecte...
38
39
40
    connectSelectVesselObservable(): void {
      this.selectedVesselService.currentVessel.subscribe(vessels => {
        this.selectedVessel = vessels;
a77940a0e   lsagona   fix a bug when yo...
41
42
43
44
        const mmsi = this.selectedVessel.getMMSI();
        if (mmsi !== undefined) {
          this.highlightMarker(Number(this.selectedVessel.getMMSI()));
        }
9bbae2409   lsagona   highlight selecte...
45
46
      });
    }
c4e00deb9   lsagona   add map
47
    initMap(): void {
72dfa80e2   lsagona   eache vessel have...
48
      this.map = L.map('map').setView([0, 0], 1);
c4e00deb9   lsagona   add map
49
      L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
287033a62   lsagona   add vessel positi...
50
51
52
53
54
        attribution: 'map',
      }).addTo(this.map);
    }
  
    addCircleMarker(message: Message, color: string): void {
9bbae2409   lsagona   highlight selecte...
55
56
57
58
59
60
61
62
      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();
a77940a0e   lsagona   fix a bug when yo...
63
      } else {
9bbae2409   lsagona   highlight selecte...
64
65
66
        circleMarker = L.circleMarker([+message.latitude, +message.longitude],
          {
            renderer: this.renderer,
a77940a0e   lsagona   fix a bug when yo...
67
            radius: 1,
9bbae2409   lsagona   highlight selecte...
68
69
70
            color
          }).addTo(this.map);
      }
a77940a0e   lsagona   fix a bug when yo...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
      if (!this.circleMarkers.has(Number(message.mmsi))){
        this.circleMarkers.set(Number(message.mmsi), new Array<CircleMarker>());
      }
      this.circleMarkers.get(Number(message.mmsi)).push(circleMarker);
    }
  
    highlightMarker(mmsi: number): void {
      if (this.highlightedMarkerId !== undefined) {
        const color = this.vessels.vessels.get(this.highlightedMarkerId).getColor();
        this.circleMarkers.get(this.highlightedMarkerId).forEach(value => value.setStyle({color, fillColor: color}));
        this.circleMarkers.get(this.highlightedMarkerId).forEach(value => value.setRadius(1));
  
      }
      this.highlightedMarkerId = mmsi;
      this.circleMarkers.get(mmsi).forEach(value => value.setStyle({fillColor: '#ff0000', color: '#ff0000', fillOpacity: 1}));
      this.circleMarkers.get(mmsi).forEach(value => value.setRadius(4));
287033a62   lsagona   add vessel positi...
87
88
89
    }
  
    updateMap(): void {
9bbae2409   lsagona   highlight selecte...
90
      this.circleMarkers.clear();
287033a62   lsagona   add vessel positi...
91
92
93
      this.vessels.vessels.forEach(((vessel, key) => {
        const color = vessel.getColor();
        vessel.messages.forEach((message) => {
287033a62   lsagona   add vessel positi...
94
95
96
          this.addCircleMarker(message, color);
        });
      }));
c666d6db6   lsagona   add map graph and...
97
98
99
    }
  
  }