MapComplete/src/UI/BigComponents/ReverseGeocoding.svelte

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
1.7 KiB
Svelte
Raw Normal View History

2023-12-21 01:46:18 +01:00
<script lang="ts">
/**
* Shows the current address when shaken
**/
import Motion from "../../Sensors/Motion"
import { Geocoding } from "../../Logic/Osm/Geocoding"
import Hotkeys from "../Base/Hotkeys"
import Translations from "../i18n/Translations"
import Locale from "../i18n/Locale"
2023-12-21 17:36:43 +01:00
import MapCenterDetails from "./MapCenterDetails.svelte"
import ThemeViewState from "../../Models/ThemeViewState"
2023-12-19 22:21:34 +01:00
2023-12-21 17:36:43 +01:00
export let state: ThemeViewState
let mapProperties = state.mapProperties
2023-12-21 01:46:18 +01:00
let lastDisplayed: Date = undefined
let currentLocation: string = undefined
2023-12-19 22:21:34 +01:00
2023-12-21 01:46:18 +01:00
async function displayLocation() {
lastDisplayed = new Date()
let result = await Geocoding.reverse(
mapProperties.location.data,
mapProperties.zoom.data,
2024-02-20 13:33:38 +01:00
Locale.language.data
2023-12-21 01:46:18 +01:00
)
let properties = result.features[0].properties
currentLocation = properties.display_name
window.setTimeout(() => {
if (properties.display_name !== currentLocation) {
return
}
currentLocation = undefined
}, 5000)
}
Motion.singleton.lastShakeEvent.addCallbackD((shaken) => {
if (lastDisplayed !== undefined && shaken.getTime() - lastDisplayed.getTime() < 2000) {
return
}
displayLocation()
})
2023-12-21 01:46:18 +01:00
Hotkeys.RegisterHotkey(
{ nomod: "q" },
Translations.t.hotkeyDocumentation.queryCurrentLocation,
() => {
displayLocation()
},
2024-02-20 13:33:38 +01:00
[Translations.t.hotkeyDocumentation.shakePhone]
2023-12-21 01:46:18 +01:00
)
2023-12-21 01:46:18 +01:00
Motion.singleton.startListening()
2023-12-19 22:21:34 +01:00
</script>
{#if currentLocation}
2023-12-21 01:46:18 +01:00
<div
aria-live="assertive"
2023-12-30 15:24:30 +01:00
class="normal-background border-interactive flex flex-col items-center rounded-full px-2"
2023-12-21 01:46:18 +01:00
>
{currentLocation}.
2023-12-30 15:24:30 +01:00
<MapCenterDetails {state} />
2023-12-19 22:21:34 +01:00
</div>
{/if}