import { Component, OnInit, ViewEncapsulation } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { Map, TileLayer, LatLng, Marker, Circle, Icon, ZoomPanOptions } from 'leaflet'; import { setInterval } from 'timers'; import {LocationService} from "../../../location.service"; @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 { private defaultLocation: LatLng = new LatLng(51.4556, 7.0116); private defaultZoom: number = 13; private zoomPanOptions: ZoomPanOptions = { animate: true, duration: 2, easeLinearity: 0.6, noMoveStart: true }; private currentShare: number; private map: Map; private tileLayer: TileLayer; private defaultIcon = new Icon({ iconUrl: require('leaflet/dist/images/marker-icon.png') }); private marker: Marker; constructor( private route: ActivatedRoute, private locationService: LocationService ) { } ngOnInit() { // Fetch share id from url this.currentShare = +this.route.snapshot.paramMap.get('id'); this.map = new Map('map'); this.map.setView(this.defaultLocation, this.defaultZoom); // this.setMapMarker(this.defaultLocation); // this.map.on('click', (e) => this.setMapMarker(e.latlng)); this.tileLayer = new TileLayer('https://cloud.locoslab.com/carta/restful-services/tile/{z}/{x}/{y}.png', { attribution: 'Maptology', maxZoom: 18 }).addTo(this.map); // 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) { // TODO: Swap with real API call. setInterval(() => { this.setMapPosition(this.locationService.getLocation(), 13); }, 2000); } } setMapPosition(latLang: LatLng, zoom: number) { this.map.panTo(latLang, this.zoomPanOptions); this.setMapMarker(latLang); } setMapMarker(latLang: LatLng) { if (this.marker) { this.marker.removeFrom(this.map); } this.marker = new Marker(latLang, { icon: this.defaultIcon }).addTo(this.map); } setMapCircle(latLang: LatLng, color: string = 'red', opacity: number = 0.5, radius: number) { const circle: Circle = new Circle(latLang, { color: color, fillColor: color, fillOpacity: opacity, radius: radius }).addTo(this.map); } onSidebarToggle() { alert('toggle'); } }