MapComplete/src/UI/BigComponents/ReverseGeocoding.svelte

53 lines
1.4 KiB
Svelte
Raw Normal View History

2023-12-19 22:21:34 +01:00
<script lang="ts">/**
* Shows the current address when shaken
**/
import Motion from "../../Sensors/Motion"
import { Geocoding } from "../../Logic/Osm/Geocoding"
import type { MapProperties } from "../../Models/MapProperties"
import Hotkeys from "../Base/Hotkeys"
import Translations from "../i18n/Translations"
import Locale from "../i18n/Locale"
2023-12-19 22:21:34 +01:00
export let mapProperties: MapProperties
let lastDisplayed: Date = undefined
let currentLocation: string = undefined
async function displayLocation() {
lastDisplayed = new Date()
let result = await Geocoding.reverse(
mapProperties.location.data,
mapProperties.zoom.data,
Locale.language.data
2023-12-19 22:21:34 +01:00
)
let properties = result.features[0].properties
currentLocation = properties.display_name
window.setTimeout(() => {
if(properties.display_name !== currentLocation){
return
}
2023-12-19 22:21:34 +01:00
currentLocation = undefined
}, 5000)
}
Motion.singleton.lastShakeEvent.addCallbackD(shaken => {
if (lastDisplayed !== undefined && shaken.getTime() - lastDisplayed.getTime() < 2000) {
return
2023-12-19 22:21:34 +01:00
}
displayLocation()
})
Hotkeys.RegisterHotkey({ nomod: "q" },
Translations.t.hotkeyDocumentation.queryCurrentLocation,
() => {
displayLocation()
})
2023-12-19 22:21:34 +01:00
Motion.singleton.startListening()
</script>
{#if currentLocation}
<div role="alert" aria-live="assertive" class="normal-background rounded-full border-interactive px-2">
2023-12-19 22:21:34 +01:00
{currentLocation}
</div>
{/if}