aboutsummaryrefslogtreecommitdiff
path: root/src/static/main.js
blob: 82d474e16d3f8edcdb7abebe7774ce6d5bb90ef8 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
var map = L.map('map').setView([55.68, 12.57], 13);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
    maxZoom: 19,
    attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);

let known_store_chains;
let icons = {};

async function fetchChains() {
    const response = await fetch("chains");
    known_store_chains = await response.json();
    icons["Unknown"] = L.icon({
        iconUrl: 'icons/Unknown.png',
        iconSize: [10, 16],
        iconAnchor: [16, 16],
    });
    for (let known_store_chain in known_store_chains) {
        icons[known_store_chain] = L.icon({
            iconUrl: `icons/${known_store_chain}.png`,
            iconAnchor: [8, 8],
        });
    }
    fetchAll()
}


function addPolygon(store) {
    let color;
    if (known_store_chains.hasOwnProperty(store.brand)) {
        color = known_store_chains[store.brand];
    } else {
        color = 'grey';
    }
    L.polygon(store.polygon, {color: color}).addTo(map);
}

function addShop(shop) {
    let icon;

    if (known_store_chains.hasOwnProperty(shop.brand)){
        icon = icons[shop.brand];
    } else {
        icon = icons["Unknown"];
    }

    L.marker([shop.lat, shop.long], {
        title: shop.name,
        icon: icon,
    }).addTo(map);
    addPolygon(shop)
}


async function fetchAll() {
  const response = await fetch("all.json");
  const shops = await response.json();
  shops.forEach((shop) => addShop(shop));
}


async function fetch_voronoi_polygons() {
  const response = await fetch("voronoi_polygons.json");
  const voronoi_polygons = await response.json();
  voronoi_polygons.forEach((polygon) => addPolygon(polygon));
}

function update(){
    if (map.getZoom() >= 14){
        //console.log('update')
    }
}

map.on('moveend', function() {
    update()
});

fetchChains()