180 lines
5.0 KiB
TypeScript
Executable File
180 lines
5.0 KiB
TypeScript
Executable File
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 { ConsoleService } from '../../../console.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 currentLocation: L.LatLng;
|
|
private currentZoom: Number;
|
|
|
|
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('../../../../assets/img/marker_normal.png'),
|
|
iconSize: [50, 64], // size of the icon
|
|
iconAnchor: [25, 63], // point of the icon which will correspond to marker's location
|
|
});
|
|
|
|
private marker: L.Marker;
|
|
|
|
private refreshInterval;
|
|
|
|
@ViewChild('shareNonExistentModal') shareNonExistentModal;
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private locationService: LocationService,
|
|
private consoleService: ConsoleService,
|
|
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);
|
|
this.setMapPosition(this.locationService.location.getValue(), 10);
|
|
}, 2000);
|
|
|
|
} 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<Location> {
|
|
return this.locationService.location;
|
|
}
|
|
|
|
getError(): BehaviorSubject<string> {
|
|
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,
|
|
});
|
|
}
|
|
}
|