summaryrefslogtreecommitdiff
path: root/script.js
blob: ec32e6e64f567ad59412c04d3f809e6b17a10ac3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const LINKS = [
    {
        label: 'Google Maps',
        url: 'https://www.google.com/maps/search/?api=1&query={lat},{lon}',
    },
    {
        label: 'Apple Maps',
        url: 'https://maps.apple.com/?q={lat},{lon}&t=m',
    },
    {
        label: 'Default app',
        url: 'geo:{lat},{lon}',
    },
    {
        label: 'OpenStreetMap',
        url: 'https://www.openstreetmap.org/?mlat={lat}&mlon={lon}&zoom=15&layers=M',
    },
];

const heading = document.getElementById('heading');

route();
// TODO: route on hashChange

function route() {
    const latLon = parseLatLon(location.hash.slice(1));

    if (!latLon) {
        heading.textContent = 'not yet implemented';
    } else {
        const linkList = document.getElementById('links');
        document.getElementById('instructions').hidden = false;

        document.title = 'Open location';
        heading.textContent = '';

        const [lat, lon] = latLon;

        for (const link of LINKS) {
            const li = document.createElement('li');
            const a = document.createElement('a');
            a.href = link.url.replace('{lat}', lat).replace('{lon}', lon);
            a.textContent = link.label;
            li.appendChild(a);
            linkList.appendChild(li);
        }
    }
}

function parseLatLon(text) {
    if (text == '') {
        return null;
    }

    let [lat, lon] = text.split(',');
    lat = parseFloat(lat);
    lon = parseFloat(lon);
    if (Number.isNaN(lat) || Number.isNaN(lon)) {
        throw Error('failed to parse latitude or longitude');
    }
    if (lat < -180 || lat > 180 || lon < -90 || lon > 90) {
        throw Error('latitude or longitude are out of range');
    }
    return [lat, lon];
}