Blame view

src/components/road-map.component.ts 6.07 KB
229df284   Yarik   Big map commit
1
  import {Component, Input, AfterViewInit, ViewChild} from '@angular/core';
e74fdc9c   Yarik   Button
2
  import * as L from 'leaflet';
dc9a0e3c   Yarik   Road fatality
3
  import { Way } from '../models/way';
229df284   Yarik   Big map commit
4
5
6
7
8
  import {WayPolyline} from "../models/way-polyline";
  import {NodeMarker} from "../models/node-marker";
  import {Node} from "../models/node";
  import {RoadService} from "../services/road.service";
  import {TdExpansionPanelComponent} from "@covalent/core/expansion-panel/expansion-panel.component";
e74fdc9c   Yarik   Button
9
10
11
12
  
  @Component({
      // tslint:disable-next-line:component-selector
      selector: 'road-map',
229df284   Yarik   Big map commit
13
      templateUrl: 'road-map.component.html',
e74fdc9c   Yarik   Button
14
15
16
  })
  export class RoadMapComponent implements AfterViewInit {
      protected isVisible: boolean = false;
229df284   Yarik   Big map commit
17
18
19
      protected isNodesVisible: boolean = false;
      protected isLegend: boolean = false;
      protected isExpanded: boolean = false;
e74fdc9c   Yarik   Button
20
      protected map: L.Map;
229df284   Yarik   Big map commit
21
22
23
      protected ways: Way[] = [];
      protected legend: string;
      protected legendSummary: string;
e74fdc9c   Yarik   Button
24
25
      @Input() identificator: string = 'mapID';
  
229df284   Yarik   Big map commit
26
27
28
29
      constructor(
          protected service: RoadService
      ) {}
  
e74fdc9c   Yarik   Button
30
31
32
33
34
      public ngAfterViewInit(): void {
          this.map = L.map(this.identificator).setView([51.505, -0.09], 13);
          L.tileLayer('https://a.tile.openstreetmap.org/{z}/{x}/{y}.png', {
              maxZoom: 18,
          }).addTo(this.map);
229df284   Yarik   Big map commit
35
36
37
38
39
40
41
          let controls: L.Control = new L.Control({
              position: 'topright'
          });
          controls.onAdd = function (map: L.Map): HTMLElement {
              return L.DomUtil.create('md-icon', 'material-icons mat-icon');
          };
          controls.addTo(this.map);
e74fdc9c   Yarik   Button
42
43
44
45
46
      }
      public showMap(): void {
          this.isVisible = true;
      }
      public getHeight(): string {
dc9a0e3c   Yarik   Road fatality
47
48
49
          return this.isVisible ? '100%' : '100%';
      }
      public setWays(ways: Way[]): void {
229df284   Yarik   Big map commit
50
51
          this.ways = this.ways.concat(ways);
          this.showWays(ways);
dc9a0e3c   Yarik   Road fatality
52
      }
229df284   Yarik   Big map commit
53
      protected showWays(ways: Way[]): void {
dc9a0e3c   Yarik   Road fatality
54
55
56
57
          let minLat: number = 0;
          let maxLat: number = 0;
          let minLon: number = 0;
          let maxLon: number = 0;
229df284   Yarik   Big map commit
58
          ways.forEach((way) => {
dc9a0e3c   Yarik   Road fatality
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
              let nodes: L.LatLng[] = [];
              way.nodes.forEach((node) => {
                  if (minLat == 0) {
                      minLat = node.lat;
                      maxLat = node.lat;
                  } else {
                      minLat = (minLat > node.lat)?node.lat:minLat;
                      maxLat = (maxLat < node.lat)?node.lat:maxLat;
                  }
                  if (minLon == 0) {
                      minLon = node.lon;
                      maxLon = node.lon;
                  } else {
                      minLon = (minLon > node.lon)?node.lon:minLon;
                      maxLon = (maxLon < node.lon)?node.lon:maxLon;
                  }
                  let latLng = node.getLatLng();
229df284   Yarik   Big map commit
76
77
                  node.marker = new NodeMarker(latLng);
                  node.marker.node = node;
dc9a0e3c   Yarik   Road fatality
78
79
                  nodes.push(latLng);
              });
229df284   Yarik   Big map commit
80
81
82
83
84
85
86
87
88
              way.polyline = new WayPolyline(nodes, {color: 'red'});
              way.polyline.way = way;
              way.polyline.addTo(this.map).on('click', (event) => {
                  console.log(event);
                  this.service.getRoadByWay(event.target.way.id).then(road => {
                      this.setWaySummary(way);
                      this.showLegend(road, 'Дорога');
                  });
              });
dc9a0e3c   Yarik   Road fatality
89
90
91
92
93
          });
          this.map.fitBounds([
              [minLat, minLon],
              [maxLat, maxLon]
          ]);
dc9a0e3c   Yarik   Road fatality
94
          this.showMap();
e74fdc9c   Yarik   Button
95
      }
229df284   Yarik   Big map commit
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
      protected showNodes(): void {
          this.ways.forEach(way => {
              way.nodes.forEach(node => {
                  node.marker.addTo(this.map).on('click', (event) => {
                      this.service.getRoadByNode(event.target.node.id).then(road => {
                          this.setNodeSummary(node);
                          this.showLegend(road, 'Дорога');
                      });
                      console.log(event);
                  });
              });
          });
      }
      protected hideNodes(): void {
          this.ways.forEach(way => {
              way.nodes.forEach(node => {
                  node.marker.remove();
              });
          });
      }
      protected toggleNodes(): void {
          if (this.isNodesVisible) {
              this.hideNodes();
              this.isNodesVisible = false;
          } else {
              this.showNodes();
              this.isNodesVisible = true
          }
      }
      protected toggleLegend(): void {
          this.isLegend = !this.isLegend;
      }
      protected showLegend(model: Object, name: string): void {
          let result: string = '';
          result += '<h3>Информация об объекте ' + name;
          if (model.hasOwnProperty('id')) {
              result += ' (id: ' + model['id'] + ')';
          }
          result += '</h3><ul>';
          for (let property in model) {
              result += '<li><strong>' + property + '</strong>: ' + (model[property]?model[property]:'Не указано') + '</li>';
          }
          result += '</ul>';
          this.isLegend = true;
          this.legend = result;
      }
      protected setNodeSummary(node: Node): void {
          let result: string = '<p>Вы нажали на точку с ID = ' + node.id + '.</p>';
          result += '<p>Координаты точки: lat = ' + node.lat + ', lon = ' + node.lon + '.</p>';
          this.legendSummary = result;
      }
      protected setWaySummary(way: Way): void {
          let result: string = '<p>Вы нажали на линию с ID = ' + way.id + '.</p>';
          result += '<p>Количество точек на линии: ' + way.nodes.length + '.</p>';
          this.legendSummary = result;
      }
      protected setLegend(legend: string) {
          this.legend = legend;
          this.isLegend = true;
      }
      public clearWays(): void {
          this.ways.forEach(way => {
              way.nodes.forEach(node => {
                  node.marker.remove();
              });
              way.polyline.remove();
          });
          this.ways = [];
          this.legend = undefined;
          this.isLegend = false;
          this.isNodesVisible = false;
      }
      protected collapsedEvent() {
          this.isExpanded = false;
      }
      protected expandedEvent() {
          this.isExpanded = true;
      }
e74fdc9c   Yarik   Button
174
  }