geomon.viewer/src/common/components/services/locate.service.ts
Arno Kaimbacher feab502c1d - add npm packages: "@fortawesome/angular-fontawesome, @fortawesome/fontawesome-svg-core, @fortawesome/free-solid-svg-icons
- add locate.service.ts, map-cache.service.ts
- add mapCache Service to map component
- notes
2021-09-08 14:40:57 +02:00

49 lines
No EOL
1.3 KiB
TypeScript

import { Injectable } from '@angular/core';
import * as L from 'leaflet';
import { MapCache } from './map-cache.service';
const LOCATION_FOUND_EVENT = 'locationfound';
const LOCATION_ERROR = 'locationerror';
const LOCATED_MARKER_ID = 'located';
@Injectable()
export class LocateService {
constructor(
protected mapCache: MapCache
) { }
public startLocate(id: string) {
const map = this.mapCache.getMap(id);
map.on(LOCATION_FOUND_EVENT, (evt: L.LocationEvent) => {
this.removeMarker(map);
const marker = L.marker(evt.latlng).addTo(map);
marker.options.title = LOCATED_MARKER_ID;
});
map.on(LOCATION_ERROR, (error) => {
console.error(error);
});
map.locate({
watch: true,
setView: true,
timeout: 30000
});
}
public stopLocate(id: string) {
const map = this.mapCache.getMap(id);
map.stopLocate();
map.off(LOCATION_FOUND_EVENT);
this.removeMarker(map);
}
private removeMarker(map: L.Map) {
map.eachLayer((entry) => {
if (entry instanceof L.Marker && entry.options.title === LOCATED_MARKER_ID) {
map.removeLayer(entry);
}
});
}
}