From e2419c19cd510dcf0b296dab1e7ccd41c3323cb5 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Sat, 24 Dec 2022 02:12:22 +0100 Subject: [PATCH] Zoom to current location if it is in view --- UI/BigComponents/GeolocationControl.ts | 24 ++++++++++++++++++++++-- UI/BigComponents/RightControls.ts | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/UI/BigComponents/GeolocationControl.ts b/UI/BigComponents/GeolocationControl.ts index a6689cff44..b9feeab5ee 100644 --- a/UI/BigComponents/GeolocationControl.ts +++ b/UI/BigComponents/GeolocationControl.ts @@ -2,13 +2,21 @@ import { VariableUiElement } from "../Base/VariableUIElement" import Svg from "../../Svg" import { UIEventSource } from "../../Logic/UIEventSource" import GeoLocationHandler from "../../Logic/Actors/GeoLocationHandler" +import { BBox } from "../../Logic/BBox" +import Loc from "../../Models/Loc" /** * Displays an icon depending on the state of the geolocation. * Will set the 'lock' if clicked twice */ export class GeolocationControl extends VariableUiElement { - constructor(geolocationHandler: GeoLocationHandler) { + constructor( + geolocationHandler: GeoLocationHandler, + state: { + locationControl: UIEventSource + currentBounds: UIEventSource + } + ) { const lastClick = new UIEventSource(undefined) const lastClickWithinThreeSecs = lastClick.map((lastClick) => { if (lastClick === undefined) { @@ -78,8 +86,20 @@ export class GeolocationControl extends VariableUiElement { return } - // A location _is_ known! Let's zoom to this location + // A location _is_ known! Let's move to this location + const currentLocation = geolocationState.currentGPSLocation.data + const inBounds = state.currentBounds.data.contains([ + currentLocation.longitude, + currentLocation.latitude, + ]) geolocationHandler.MoveMapToCurrentLocation() + if (inBounds) { + const lc = state.locationControl.data + state.locationControl.setData({ + ...lc, + zoom: lc.zoom + 3, + }) + } if (lastClickWithinThreeSecs.data && geolocationState.permission.data === "granted") { geolocationState.isLocked.setData(true) diff --git a/UI/BigComponents/RightControls.ts b/UI/BigComponents/RightControls.ts index 046d5b3c8a..2ce468065b 100644 --- a/UI/BigComponents/RightControls.ts +++ b/UI/BigComponents/RightControls.ts @@ -14,7 +14,7 @@ export default class RightControls extends Combine { geolocationHandler: GeoLocationHandler ) { const geolocationButton = new Toggle( - new MapControlButton(new GeolocationControl(geolocationHandler), { + new MapControlButton(new GeolocationControl(geolocationHandler, state), { dontStyle: true, }).SetClass("p-1"), undefined,