mapcomplete/Logic/Actors/GeoLocationHandler.ts

162 lines
5.1 KiB
TypeScript
Raw Normal View History

import * as L from "leaflet";
import {UIEventSource} from "../UIEventSource";
2020-07-29 22:59:08 +00:00
import {UIElement} from "../../UI/UIElement";
2020-07-31 02:58:58 +00:00
import {Utils} from "../../Utils";
2020-11-05 11:28:02 +00:00
import Svg from "../../Svg";
2021-01-02 23:19:42 +00:00
import Img from "../../UI/Base/Img";
export class GeoLocationHandler extends UIElement {
private readonly _isActive: UIEventSource<boolean> = new UIEventSource<boolean>(false);
private readonly _permission: UIEventSource<string> = new UIEventSource<string>("");
2021-01-02 20:03:40 +00:00
private _marker: L.Marker;
private readonly _hasLocation: UIEventSource<boolean>;
2021-01-02 20:03:40 +00:00
private readonly _currentGPSLocation: UIEventSource<{ latlng: any; accuracy: number }>;
private readonly _leafletMap: UIEventSource<L.Map>;
private readonly _featureSwitch: UIEventSource<boolean>;
2021-01-02 20:03:40 +00:00
constructor(currentGPSLocation: UIEventSource<{ latlng: any; accuracy: number }>,
leafletMap: UIEventSource<L.Map>,
featureSwitch: UIEventSource<boolean>) {
super(undefined);
2021-01-02 20:03:40 +00:00
this._currentGPSLocation = currentGPSLocation;
this._leafletMap = leafletMap;
this._featureSwitch = featureSwitch;
this._hasLocation = currentGPSLocation.map((location) => location !== undefined);
var self = this;
import("../../vendor/Leaflet.AccuratePosition.js").then(() => {
self.init();
})
}
public init() {
this.ListenTo(this._hasLocation);
this.ListenTo(this._isActive);
2020-06-28 21:33:48 +00:00
this.ListenTo(this._permission);
const self = this;
function onAccuratePositionProgress(e) {
2021-01-02 20:03:40 +00:00
self._currentGPSLocation.setData({latlng: e.latlng, accuracy: e.accuracy});
}
function onAccuratePositionFound(e) {
2021-01-02 20:03:40 +00:00
self._currentGPSLocation.setData({latlng: e.latlng, accuracy: e.accuracy});
}
function onAccuratePositionError(e) {
console.log("onerror", e.message);
}
2021-01-02 20:03:40 +00:00
const map = this._leafletMap.data;
map.on('accuratepositionprogress', onAccuratePositionProgress);
map.on('accuratepositionfound', onAccuratePositionFound);
map.on('accuratepositionerror', onAccuratePositionError);
2020-11-17 01:22:48 +00:00
2021-01-02 20:03:40 +00:00
this._currentGPSLocation.addCallback((location) => {
2020-11-17 01:22:48 +00: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
})
2021-01-02 20:03:40 +00:00
const newMarker = L.marker(location.latlng, {icon: icon});
newMarker.addTo(map);
if (self._marker !== undefined) {
map.removeLayer(self._marker);
}
self._marker = newMarker;
});
2020-07-29 18:55:25 +00:00
navigator?.permissions?.query({name: 'geolocation'})
?.then(function (status) {
2020-06-28 21:33:48 +00: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);
}
InnerRender(): string {
2021-01-02 20:03:40 +00:00
if (!this._featureSwitch.data) {
return "";
}
2021-01-02 20:03:40 +00:00
if (this._hasLocation.data) {
2020-11-05 11:28:02 +00:00
return Svg.crosshair_blue_img;
}
if (this._isActive.data) {
2020-11-05 11:28:02 +00:00
return Svg.crosshair_blue_center_img;
}
2020-11-05 11:28:02 +00:00
return Svg.crosshair_img;
}
2021-01-02 20:03:40 +00:00
InnerUpdate(htmlElement: HTMLElement) {
super.InnerUpdate(htmlElement);
const self = this;
htmlElement.onclick = function () {
self.StartGeolocating(19);
}
htmlElement.oncontextmenu = function (e) {
self.StartGeolocating(15);
e.preventDefault();
return false;
}
}
private StartGeolocating(zoomlevel = 19) {
const self = this;
2021-01-02 20:03:40 +00:00
const map : any = this._leafletMap.data;
2020-06-28 21:33:48 +00:00
if (self._permission.data === "denied") {
return "";
}
2021-01-02 20:03:40 +00:00
if (this._currentGPSLocation.data !== undefined) {
this._leafletMap.data.setView(
this._currentGPSLocation.data.latlng, zoomlevel
);
}
console.log("Searching location using GPS")
map.findAccuratePosition({
2020-06-28 21:33:48 +00:00
maxWait: 10000, // defaults to 10000
desiredAccuracy: 50 // defaults to 20
});
2021-01-02 20:03:40 +00:00
2020-06-28 21:33:48 +00:00
if (!self._isActive.data) {
self._isActive.setData(true);
2020-07-31 02:58:58 +00:00
Utils.DoEvery(60000, () => {
if (document.visibilityState !== "visible") {
console.log("Not starting gps: document not visible")
return;
}
map.findAccuratePosition({
2020-06-28 21:33:48 +00:00
maxWait: 10000, // defaults to 10000
desiredAccuracy: 50 // defaults to 20
});
})
}
}
}