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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
<div class="section">
<div id="loadingmap" class="preloader-wrapper small active" style="left: 45%;">
<div class="spinner-layer spinner-blue-only">
<div class="circle-clipper left">
<div class="circle"></div>
</div><div class="gap-patch">
<div class="circle"></div>
</div><div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
</div>
<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 loadFallback() {
if (typeof L === 'undefined') {
console.log('loading mapbox fallback files');
$('<link>').appendTo('head')
.attr({type: 'text/css', rel: 'stylesheet'})
.attr('href', 'https://api.mapbox.com/mapbox.js/v2.4.0/mapbox.css');
$.getScript('https://api.mapbox.com/mapbox.js/v2.4.0/mapbox.js', initFallback);
} else {
console.log('mapbox fallback files already present');
initFallback();
}
}
function initMapbox() {
if (mapboxgl.supported() === false) {
console.log('MapboxGL unsupported, loading fallback');
loadFallback();
return;
}
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 initFallback() {
console.log('creating new map fallback instance');
var lon = parseFloat({{ .Get "lon" }}),
lat = parseFloat({{ .Get "lat" }}),
zoom= parseFloat({{ .Get "zoom" }});
if (typeof map !== 'undefined') {
console.log('killed old map for fallback');
map.remove();
}
L.mapbox.accessToken = 'pk.eyJ1IjoiY29kZS13dnMiLCJhIjoiY2lzNHM4bWUwMDAwdTJ0cDBjYms5em9hYSJ9.VLxxJ6aTbWUbQchg58tgkA';
map = L.mapbox.map('mapview')
.setView([lat, lon], zoom);
var layer = L.mapbox.tileLayer('mapbox.streets').addTo(map);
layer.on('ready', showFallback);
}
/* ! mapboxgl: [lon, lat] - mapboxjs: [lat, lon]… */
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"
}
});
}
$('#loadingmap').hide();
$('#mapview').show();
}
function showFallback() {
var lon = parseFloat({{ .Get "lon" }}),
lat = parseFloat({{ .Get "lat" }}),
zoom= parseFloat({{ .Get "zoom" }});
console.log('updating fallback map data', [lon, lat, zoom]);
map.setView([lat, lon], zoom);
L.marker([lat, lon]).addTo(map);
$('#loadingmap').hide();
$('#mapview').show();
}
</script>
</div>
|