import { Component, OnInit, ViewEncapsulation, ViewChild } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { Location as BrowserLocation } from '@angular/common'; import * as L from 'leaflet'; import 'leaflet-easybutton'; import { setInterval, clearInterval } from 'timers'; import { LocationService } from '../../../location.service'; import { Location } from '../../../../api/models/location.model'; import { LatLng } from 'leaflet'; import { ToastrService } from 'ngx-toastr'; import { error } from 'util'; import { OnDestroy } from '@angular/core/src/metadata/lifecycle_hooks'; @Component({ selector: 'app-map', templateUrl: './map.component.html', styleUrls: ['./map.component.scss', '../../../../../node_modules/leaflet/dist/leaflet.css'], encapsulation: ViewEncapsulation.None }) export class MapComponent implements OnInit, OnDestroy { private defaultLocation: L.LatLng = new L.LatLng(51.4556, 7.0116); private defaultZoom = 13; private zoomPanOptions: L.ZoomPanOptions = { animate: true, duration: 2, easeLinearity: 0.6, noMoveStart: true }; private currentShare: string; private map: L.Map; private tileLayer: L.TileLayer; private defaultIcon = new L.Icon({ iconUrl: require('leaflet/dist/images/marker-icon.png') }); private marker: L.Marker; private refreshInterval; @ViewChild('shareNonExistentModal') shareNonExistentModal; constructor( private route: ActivatedRoute, private locationService: LocationService, private toastService: ToastrService, private browser_location: BrowserLocation ) { } ngOnInit() { // Fetch share id from url this.currentShare = this.route.snapshot.paramMap.get('id'); this.map = new L.Map('map'); this.map.setView(this.defaultLocation, this.defaultZoom); this.tileLayer = new L.TileLayer('https://cloud.locoslab.com/carta/restful-services/tile/{z}/{x}/{y}.png', { attribution: 'Maptology', maxZoom: 18 }).addTo(this.map); L.easyButton('fa-globe', this.onSidebarToggle).addTo(this.map); // Initialize marker // this.marker.bindTooltip('Tooltip', {sticky: true}); this.setMapMarker(this.defaultLocation); // If there is a given shareId in the URL, fetch the location from the API and // start to update every x seconds if (this.currentShare) { // Query the API once to check if the share exists this.locationService.getLocation(this.currentShare); // If there are errors display a toast this.getError().subscribe(e => { if (e !== null) { // setTimeout(() => this.showError('Error', e)); setTimeout(() => this.shareNonExistentModal.show()); } }); // Query the API every 2 seconds this.refreshInterval = setInterval(() => { this.locationService.getLocation(this.currentShare); }, 2000); // subscribe to location updates this.getLocation().subscribe(location => { if (location !== null) { this.setMapPosition(location, 10); } }); } else { setTimeout(() => this.showWarning('Warning', 'URL does not contain a valid share!')); } } ngOnDestroy() { if (this.refreshInterval) { clearInterval(this.refreshInterval); } } setMapPosition(location: Location, zoom: number) { const latLang = new LatLng(location.coordinate.latitude, location.coordinate.longitude); this.map.panTo(latLang, this.zoomPanOptions); this.setMapMarker(latLang); } setMapMarker(latLang: L.LatLng) { if (this.marker) { this.marker.removeFrom(this.map); } this.marker = new L.Marker(latLang, { icon: this.defaultIcon }).addTo(this.map); } setMapCircle(latLang: L.LatLng, color: string = 'red', opacity: number = 0.5, radius: number) { const circle = new L.Circle(latLang, { color: color, fillColor: color, fillOpacity: opacity, radius: radius }).addTo(this.map); } onModalOkClick() { this.browser_location.back(); } onSidebarToggle() { alert('toggle'); } getLocation(): BehaviorSubject { return this.locationService.location; } getError(): BehaviorSubject { return this.locationService.error; } // success toast showSuccess(title: string, message: string) { this.toastService.success(message, title, { progressBar: true, closeButton: true }); } // error toast showError(title: string, message: string) { this.toastService.error(message, title, { progressBar: true, closeButton: true }); } // warning toast showWarning(title: string, message: string) { this.toastService.warning(message, title, { progressBar: true, closeButton: true, }); } }