From abfc5424a31c33c2feb1a9bafaa299428b7b496b Mon Sep 17 00:00:00 2001 From: tim-herbst Date: Sun, 3 Jan 2021 15:16:44 +0100 Subject: [PATCH] fix error with auto-refresh * if layer is toggled with auto-refresh, the layer will be drawn after refresh --- .../auto-refresh/auto-refresh.component.ts | 4 ++-- .../frontend/src/app/service/map.service.ts | 23 ++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/projects/project-3/frontend/src/app/map/auto-refresh/auto-refresh.component.ts b/projects/project-3/frontend/src/app/map/auto-refresh/auto-refresh.component.ts index 477d29f..7156d5a 100644 --- a/projects/project-3/frontend/src/app/map/auto-refresh/auto-refresh.component.ts +++ b/projects/project-3/frontend/src/app/map/auto-refresh/auto-refresh.component.ts @@ -23,9 +23,9 @@ export class AutoRefreshComponent implements OnInit, OnDestroy { ngOnInit(): void { this.interval = setInterval(() => { if (this.isFlagActive) { - this.map.autoRefresh(); + this.map.autoRefresh().catch(error => console.log(error)); } - }, 30000); + }, 10000); } ngOnDestroy(): void { diff --git a/projects/project-3/frontend/src/app/service/map.service.ts b/projects/project-3/frontend/src/app/service/map.service.ts index daee81b..381aa74 100644 --- a/projects/project-3/frontend/src/app/service/map.service.ts +++ b/projects/project-3/frontend/src/app/service/map.service.ts @@ -7,6 +7,7 @@ import {environment} from '../../environments/environment'; import {PopUpService} from './pop-up.service'; import {IMapBikePoint} from './domain/map-bike-point'; import {IDashboardCommonBikePoint} from './domain/dashboard-common-bike-point'; +import {writeErrorToLogFile} from "@angular/cli/utilities/log-file"; const createIcon = color => L.icon({ @@ -25,6 +26,7 @@ export class MapService { public miniMap; bikePoints: Array = []; mapOverlays: any = {}; + layerToDisplay: any = {}; miniMapMarker: L.layerGroup; markerLayer = []; polylineLayer = []; @@ -40,7 +42,15 @@ export class MapService { } public async autoRefresh(): Promise { + this.layerToDisplay = {}; for (const name in this.mapOverlays) { + if (this.map.hasLayer(this.mapOverlays[name])) { + if (this.mapOverlays.Heatmap === this.mapOverlays[name]) { + this.layerToDisplay.Heatmap = this.mapOverlays[name]; + } else if (this.mapOverlays.Accidents === this.mapOverlays[name]) { + this.layerToDisplay.Accidents = this.mapOverlays[name]; + } + } this.map.removeLayer(this.mapOverlays[name]); } await this.drawStationMarkers(); @@ -102,11 +112,14 @@ export class MapService { bikePoint.lon, bikePoint.status.NbBikes ])); - - this.mapOverlays.Heatmap = L.heatLayer(heatPoints, { + const heatmap = L.heatLayer(heatPoints, { max: 5, radius: 90 }); + if (this.layerToDisplay.Heatmap) { + this.map.addLayer(heatmap); + } + this.mapOverlays.Heatmap = heatmap; } public drawAccidents(): void { @@ -124,7 +137,11 @@ export class MapService { }); accidents.push(accidentMarker); } - this.mapOverlays.Accidents = L.layerGroup(accidents); + const accidentLayer = L.layerGroup(accidents); + if (this.layerToDisplay.Accidents) { + this.map.addLayer(accidentLayer); + } + this.mapOverlays.Accidents = accidentLayer; this.drawMapControl(); }); }