Blame view

src/components/road-map.component.ts 6.23 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";
7462a6be   Yarik   Authority and con...
9
  import {Road} from "../models/road";
e74fdc9c   Yarik   Button
10
11
12
13
  
  @Component({
      // tslint:disable-next-line:component-selector
      selector: 'road-map',
229df284   Yarik   Big map commit
14
      templateUrl: 'road-map.component.html',
e74fdc9c   Yarik   Button
15
16
17
  })
  export class RoadMapComponent implements AfterViewInit {
      protected isVisible: boolean = false;
229df284   Yarik   Big map commit
18
19
20
      protected isNodesVisible: boolean = false;
      protected isLegend: boolean = false;
      protected isExpanded: boolean = false;
e74fdc9c   Yarik   Button
21
      protected map: L.Map;
229df284   Yarik   Big map commit
22
23
24
      protected ways: Way[] = [];
      protected legend: string;
      protected legendSummary: string;
7462a6be   Yarik   Authority and con...
25
      protected selectedRoad: Road;
e74fdc9c   Yarik   Button
26
27
      @Input() identificator: string = 'mapID';
  
229df284   Yarik   Big map commit
28
29
30
31
      constructor(
          protected service: RoadService
      ) {}
  
e74fdc9c   Yarik   Button
32
33
34
35
36
      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
37
38
39
40
41
42
43
          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
44
45
46
47
48
      }
      public showMap(): void {
          this.isVisible = true;
      }
      public getHeight(): string {
dc9a0e3c   Yarik   Road fatality
49
50
51
          return this.isVisible ? '100%' : '100%';
      }
      public setWays(ways: Way[]): void {
229df284   Yarik   Big map commit
52
53
          this.ways = this.ways.concat(ways);
          this.showWays(ways);
dc9a0e3c   Yarik   Road fatality
54
      }
229df284   Yarik   Big map commit
55
      protected showWays(ways: Way[]): void {
dc9a0e3c   Yarik   Road fatality
56
57
58
59
          let minLat: number = 0;
          let maxLat: number = 0;
          let minLon: number = 0;
          let maxLon: number = 0;
229df284   Yarik   Big map commit
60
          ways.forEach((way) => {
dc9a0e3c   Yarik   Road fatality
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
              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
78
79
                  node.marker = new NodeMarker(latLng);
                  node.marker.node = node;
dc9a0e3c   Yarik   Road fatality
80
81
                  nodes.push(latLng);
              });
229df284   Yarik   Big map commit
82
83
84
85
86
              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 => {
7462a6be   Yarik   Authority and con...
87
                      this.selectedRoad = road;
229df284   Yarik   Big map commit
88
89
90
91
                      this.setWaySummary(way);
                      this.showLegend(road, 'Дорога');
                  });
              });
dc9a0e3c   Yarik   Road fatality
92
93
94
95
96
          });
          this.map.fitBounds([
              [minLat, minLon],
              [maxLat, maxLon]
          ]);
dc9a0e3c   Yarik   Road fatality
97
          this.showMap();
e74fdc9c   Yarik   Button
98
      }
229df284   Yarik   Big map commit
99
100
101
102
103
      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 => {
7462a6be   Yarik   Authority and con...
104
                          this.selectedRoad = road;
229df284   Yarik   Big map commit
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
174
175
176
177
                          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
178
  }