summaryrefslogtreecommitdiff
path: root/layouts/shortcodes/map.html
blob: 17eab9164ccc5efcb13c993e08b51933b58498d1 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<div class="section">
    <div id="mapview" style="width: 100%; height: 40em;"></div>
    <script type="text/javascript">
        var map,
            mapboxReadyAttached;

        if (typeof $ === 'undefined') {
            document.addEventListener('DOMContentLoaded', loadMapbox);
        }
        if (typeof mapboxReadyAttached === 'undefined') {
            mapboxReadyAttached = true;
            document.addEventListener('pageReady', loadMapbox);
        }

        function loadMapbox() {
            if ($('#mapview').length == 0) return; // map is inactive

            if (typeof mapboxgl === 'undefined') {
                console.log('loading mapbox files');
                $('<link>').appendTo('head')
                  .attr({type: 'text/css', rel: 'stylesheet'})
                  .attr('href', 'https://api.mapbox.com/mapbox-gl-js/v0.22.1/mapbox-gl.css');
                $.getScript('https://api.mapbox.com/mapbox-gl-js/v0.23.0/mapbox-gl.js', initMapbox);
            } else {
                console.log('mapbox files already present');
                initMapbox();
            }
        }

        function initMapbox() {
            console.log('creating new map instance');
            var lon = parseFloat({{ .Get "lon" }}),
                lat = parseFloat({{ .Get "lat" }}),
                zoom= parseFloat({{ .Get "zoom" }});

            if (typeof map !== 'undefined') {
                console.log('killed old map');
                map.remove();
            }

            mapboxgl.accessToken = 'pk.eyJ1IjoiY29kZS13dnMiLCJhIjoiY2lzNHM4bWUwMDAwdTJ0cDBjYms5em9hYSJ9.VLxxJ6aTbWUbQchg58tgkA';
            map = new mapboxgl.Map({
                pitch: 60,
                bearing: 45,
                container: 'mapview',
                style: 'mapbox://styles/mapbox/basic-v9',
                center: [lon, lat],
                zoom: zoom
            });
            map.on('load', showMapbox);
        }

        function showMapbox() {
            var lon = parseFloat({{ .Get "lon" }}),
                lat = parseFloat({{ .Get "lat" }}),
                zoom= parseFloat({{ .Get "zoom" }});

            console.log('updating map data', [lon, lat, zoom]);

            map.setCenter([lon, lat]);
            map.setZoom(zoom);

            var markers = {
                "type": "FeatureCollection",
                "features": [{
                    "type": "Feature",
                    "properties": {
                        "icon": "fast-food"
                    },
                    "geometry": {
                        "type": "Point",
                        "coordinates": [lon, lat]
                    }
                }]
            };

            if (!map.getSource('markers')) {
                map.addSource('markers', {
                    'type': 'geojson',
                    'data': markers
                });
                map.addLayer({
                    "id": "markers",
                    "type": "symbol",
                    "source": "markers",
                    "layout": {
                        "icon-image": "{icon}-15"
                    }
                });
            }
        }
    </script>
    </div>