Mapbox/MapLibre how to save the latest location.hash updates

27 views Asked by At

While the camera is moving around the map, the hash that is in the URL is updated, I need to save this value so that when I return to the map I will be in the same place as before I left

I used this code, the latest changes are saved in sessionStorage, but when switching to another address of my application and returning, sessionStorage was updated to the initial value, and did not accept the necessary past saves

    function initLocationHash () {
        let newHash = window.location.hash
        sessionStorage.setItem('locationHash', JSON.stringify(newHash))
    }

    window.addEventListener('DOMContentLoaded', function () {
        let storedHash = JSON.parse(sessionStorage.getItem('locationHash'));

        if (storedHash) {
            window.location.hash = storedHash
        }
        console.log(storedHash, 'storedHash')
    })

    setInterval(function () {
        let storedHash = JSON.parse(sessionStorage.getItem('locationHash'));
        let currentHash = window.location.hash
        if (currentHash !== storedHash) {

            initLocationHash()
        }
    }, 100)
0

There are 0 answers