2020-06-28 02:42:22 +02:00
|
|
|
import {Basemap} from "./Basemap";
|
|
|
|
import {UIEventSource} from "../UI/UIEventSource";
|
|
|
|
import {UIElement} from "../UI/UIElement";
|
|
|
|
import L from "leaflet";
|
2020-06-28 23:33:48 +02:00
|
|
|
import {Helpers} from "../Helpers";
|
2020-06-28 02:42:22 +02:00
|
|
|
|
|
|
|
export class GeoLocationHandler extends UIElement {
|
|
|
|
|
|
|
|
currentLocation: UIEventSource<{
|
|
|
|
latlng: number,
|
|
|
|
accuracy: number
|
|
|
|
}> = new UIEventSource<{ latlng: number, accuracy: number }>(undefined);
|
|
|
|
|
|
|
|
private _isActive: UIEventSource<boolean> = new UIEventSource<boolean>(false);
|
2020-06-28 23:33:48 +02:00
|
|
|
private _permission: UIEventSource<string> = new UIEventSource<string>("");
|
2020-06-28 02:42:22 +02:00
|
|
|
private _map: Basemap;
|
|
|
|
private _marker: any;
|
|
|
|
|
|
|
|
constructor(map: Basemap) {
|
|
|
|
super(undefined);
|
|
|
|
this._map = map;
|
|
|
|
this.ListenTo(this.currentLocation);
|
|
|
|
this.ListenTo(this._isActive);
|
2020-06-28 23:33:48 +02:00
|
|
|
this.ListenTo(this._permission);
|
2020-06-28 02:42:22 +02:00
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
|
|
|
|
function onAccuratePositionProgress(e) {
|
|
|
|
console.log(e.accuracy);
|
|
|
|
console.log(e.latlng);
|
2020-06-28 23:33:48 +02:00
|
|
|
self.currentLocation.setData({latlng: e.latlng, accuracy: e.accuracy});
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onAccuratePositionFound(e) {
|
|
|
|
console.log(e.accuracy);
|
|
|
|
console.log(e.latlng);
|
|
|
|
self.currentLocation.setData({latlng: e.latlng, accuracy: e.accuracy});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onAccuratePositionError(e) {
|
|
|
|
console.log("onerror", e.message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
map.map.on('accuratepositionprogress', onAccuratePositionProgress);
|
|
|
|
map.map.on('accuratepositionfound', onAccuratePositionFound);
|
|
|
|
map.map.on('accuratepositionerror', onAccuratePositionError);
|
|
|
|
|
|
|
|
|
|
|
|
const icon = L.icon(
|
|
|
|
{
|
|
|
|
iconUrl: './assets/crosshair-blue.svg',
|
|
|
|
iconSize: [40, 40], // size of the icon
|
|
|
|
iconAnchor: [20, 20], // point of the icon which will correspond to marker's location
|
|
|
|
})
|
|
|
|
|
|
|
|
this.currentLocation.addCallback((location) => {
|
|
|
|
const newMarker = L.marker(location.latlng, {icon: icon});
|
|
|
|
newMarker.addTo(map.map);
|
|
|
|
|
|
|
|
if (self._marker !== undefined) {
|
|
|
|
map.map.removeLayer(self._marker);
|
|
|
|
}
|
|
|
|
self._marker = newMarker;
|
|
|
|
});
|
|
|
|
|
2020-07-29 20:55:25 +02:00
|
|
|
navigator?.permissions?.query({name: 'geolocation'})
|
|
|
|
?.then(function (status) {
|
2020-06-28 23:33:48 +02:00
|
|
|
console.log("Geolocation is already", status)
|
|
|
|
if (status.state === "granted") {
|
|
|
|
self.StartGeolocating();
|
|
|
|
}
|
|
|
|
self._permission.setData(status.state);
|
|
|
|
status.onchange = function () {
|
|
|
|
self._permission.setData(status.state);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.HideOnEmpty(true);
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
|
|
|
|
2020-07-26 02:01:34 +02:00
|
|
|
InnerRender(): string {
|
2020-06-28 02:42:22 +02:00
|
|
|
if (this.currentLocation.data) {
|
2020-07-27 13:04:38 +02:00
|
|
|
return "<img src='assets/crosshair-blue.png' alt='locate me'>";
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
|
|
|
if (this._isActive.data) {
|
2020-07-27 13:04:38 +02:00
|
|
|
return "<img src='assets/crosshair-blue-center.png' alt='locate me'>";
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:04:38 +02:00
|
|
|
return "<img src='assets/crosshair.png' alt='locate me'>";
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-28 23:33:48 +02:00
|
|
|
private StartGeolocating() {
|
2020-06-28 02:42:22 +02:00
|
|
|
const self = this;
|
2020-06-28 23:33:48 +02:00
|
|
|
|
|
|
|
if (self._permission.data === "denied") {
|
|
|
|
return "";
|
|
|
|
}
|
2020-06-28 02:42:22 +02:00
|
|
|
if (self.currentLocation.data !== undefined) {
|
|
|
|
self._map.map.flyTo(self.currentLocation.data.latlng, 18);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
console.log("Searching location using GPS")
|
|
|
|
self._map.map.findAccuratePosition({
|
2020-06-28 23:33:48 +02:00
|
|
|
maxWait: 10000, // defaults to 10000
|
|
|
|
desiredAccuracy: 50 // defaults to 20
|
2020-06-28 02:42:22 +02:00
|
|
|
});
|
2020-06-28 23:33:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (!self._isActive.data) {
|
|
|
|
self._isActive.setData(true);
|
|
|
|
Helpers.DoEvery(60000, () => {
|
2020-07-26 02:01:34 +02:00
|
|
|
|
|
|
|
if(document.visibilityState !== "visible"){
|
|
|
|
console.log("Not starting gps: document not visible")
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-28 23:33:48 +02:00
|
|
|
self._map.map.findAccuratePosition({
|
|
|
|
maxWait: 10000, // defaults to 10000
|
|
|
|
desiredAccuracy: 50 // defaults to 20
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
InnerUpdate(htmlElement: HTMLElement) {
|
|
|
|
super.InnerUpdate(htmlElement);
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
htmlElement.onclick = function () {
|
|
|
|
self.StartGeolocating();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|