2020-10-02 19:00:24 +02:00
|
|
|
import * as L from "leaflet";
|
2020-08-17 17:23:15 +02:00
|
|
|
import {UIEventSource} from "../UIEventSource";
|
2020-07-30 00:59:08 +02:00
|
|
|
import {UIElement} from "../../UI/UIElement";
|
2020-10-02 19:00:24 +02:00
|
|
|
import State from "../../State";
|
2020-07-31 04:58:58 +02:00
|
|
|
import {Utils} from "../../Utils";
|
2020-08-08 02:16:42 +02:00
|
|
|
import {Basemap} from "./Basemap";
|
2020-11-05 12:28:02 +01:00
|
|
|
import Svg from "../../Svg";
|
|
|
|
import {Img} from "../../UI/Img";
|
2020-06-28 02:42:22 +02:00
|
|
|
|
|
|
|
export class GeoLocationHandler extends UIElement {
|
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
private readonly _isActive: UIEventSource<boolean> = new UIEventSource<boolean>(false);
|
|
|
|
private readonly _permission: UIEventSource<string> = new UIEventSource<string>("");
|
2020-06-28 02:42:22 +02:00
|
|
|
private _marker: any;
|
2020-08-30 01:13:18 +02:00
|
|
|
private readonly _hasLocation: UIEventSource<boolean>;
|
2020-06-28 02:42:22 +02:00
|
|
|
|
2020-07-31 01:45:54 +02:00
|
|
|
constructor() {
|
2020-06-28 02:42:22 +02:00
|
|
|
super(undefined);
|
2020-07-31 01:45:54 +02:00
|
|
|
this._hasLocation = State.state.currentGPSLocation.map((location) => location !== undefined);
|
|
|
|
this.ListenTo(this._hasLocation);
|
2020-06-28 02:42:22 +02:00
|
|
|
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-07-31 01:45:54 +02:00
|
|
|
State.state.currentGPSLocation.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);
|
2020-07-31 01:45:54 +02:00
|
|
|
State.state.currentGPSLocation.setData({latlng: e.latlng, accuracy: e.accuracy});
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onAccuratePositionError(e) {
|
|
|
|
console.log("onerror", e.message);
|
2020-07-31 01:45:54 +02:00
|
|
|
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-08 02:16:42 +02:00
|
|
|
const bm : Basemap = State.state.bm;
|
|
|
|
const map = bm.map;
|
2020-07-31 01:45:54 +02:00
|
|
|
map.on('accuratepositionprogress', onAccuratePositionProgress);
|
|
|
|
map.on('accuratepositionfound', onAccuratePositionFound);
|
|
|
|
map.on('accuratepositionerror', onAccuratePositionError);
|
2020-06-28 02:42:22 +02:00
|
|
|
|
2020-11-17 02:22:48 +01:00
|
|
|
|
2020-06-28 02:42:22 +02:00
|
|
|
|
2020-07-31 01:45:54 +02:00
|
|
|
State.state.currentGPSLocation.addCallback((location) => {
|
2020-11-17 02:22:48 +01:00
|
|
|
|
|
|
|
const color = getComputedStyle(document.body).getPropertyValue("--catch-detail-color")
|
|
|
|
const icon = L.icon(
|
|
|
|
{
|
|
|
|
iconUrl: Img.AsData(Svg.crosshair.replace(/#000000/g, color)),
|
|
|
|
iconSize: [40, 40], // size of the icon
|
|
|
|
iconAnchor: [20, 20], // point of the icon which will correspond to marker's location
|
|
|
|
})
|
|
|
|
|
2020-06-28 02:42:22 +02:00
|
|
|
const newMarker = L.marker(location.latlng, {icon: icon});
|
2020-08-08 02:16:42 +02:00
|
|
|
newMarker.addTo(map);
|
2020-06-28 02:42:22 +02:00
|
|
|
|
|
|
|
if (self._marker !== undefined) {
|
2020-07-31 01:45:54 +02:00
|
|
|
map.removeLayer(self._marker);
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
|
|
|
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-08-08 14:43:48 +02:00
|
|
|
if(!State.state.featureSwitchGeolocation.data){
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-07-31 01:45:54 +02:00
|
|
|
if (this._hasLocation.data) {
|
2020-11-05 12:28:02 +01:00
|
|
|
return Svg.crosshair_blue_img;
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
|
|
|
if (this._isActive.data) {
|
2020-11-05 12:28:02 +01:00
|
|
|
return Svg.crosshair_blue_center_img;
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
|
|
|
|
2020-11-05 12:28:02 +01:00
|
|
|
return Svg.crosshair_img;
|
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-07-31 01:45:54 +02:00
|
|
|
const map = State.state.bm.map;
|
2020-06-28 23:33:48 +02:00
|
|
|
if (self._permission.data === "denied") {
|
|
|
|
return "";
|
|
|
|
}
|
2020-07-31 01:45:54 +02:00
|
|
|
if (State.state.currentGPSLocation.data !== undefined) {
|
|
|
|
map.flyTo(State.state.currentGPSLocation.data.latlng, 18);
|
2020-06-28 02:42:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
console.log("Searching location using GPS")
|
2020-07-31 01:45:54 +02:00
|
|
|
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);
|
2020-07-31 04:58:58 +02:00
|
|
|
Utils.DoEvery(60000, () => {
|
2020-07-31 01:45:54 +02:00
|
|
|
|
|
|
|
if (document.visibilityState !== "visible") {
|
2020-07-26 02:01:34 +02:00
|
|
|
console.log("Not starting gps: document not visible")
|
|
|
|
return;
|
|
|
|
}
|
2020-07-31 01:45:54 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
InnerUpdate(htmlElement: HTMLElement) {
|
|
|
|
super.InnerUpdate(htmlElement);
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
htmlElement.onclick = function () {
|
|
|
|
self.StartGeolocating();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|