-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
147 lines (122 loc) · 4.22 KB
/
main.js
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
import { registerSW } from 'virtual:pwa-register'
// Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer
import 'elm-pep';
import 'ol/ol.css';
import Map from 'ol/Map';
import TileLayer from 'ol/layer/Tile';
import View from 'ol/View';
import WMTS from 'ol/source/WMTS';
import WMTSTileGrid from 'ol/tilegrid/WMTS';
import {fromLonLat, get as getProjection} from 'ol/proj';
import {getWidth} from 'ol/extent';
import './style.css';
import { installGeolocation, setGeolocationTracking, centerOnGeolocation } from './geolocation';
import { installCache, onlineOrCacheTileLoadFunction } from './cache';
import { installCacheIntersection } from './cache-intersection';
// ------------------------------
// PWA Service worker online / offline
// ------------------------------
const updateSW = registerSW({
onNeedRefresh() {
if (confirm('Une version plus récente de l\'application est disponible. Recharger ?')) {
updateSW();
}
},
onOfflineReady() {
alert('L\'application est prête à fonctionner sans réseau.');
},
})
// ------------------------------
// UI
// ------------------------------
document.querySelector('#app').innerHTML = `
<h1>Photographies aériennes <span id="status"></span></h1>
<div class="toolbar">
<span>Position</span>
<button id="position" title="Suivre la position"><span id="positionIcon">✕</span> Suivre</button>
<button id="center-position" title="Centrer sur la position">⌖ Centrer</button>
<button id="goto-dijon" title="Dijon">🦉 Dijon</button>
</div>
<div id="map" class="map"></div>
`
// ------------------------------
// Attach actions
// ------------------------------
document.getElementById('goto-dijon').onclick = function() {
// Calculé à partir de la fonction suivante (ajouter window.map = map à la fin de ce fichier)
// map.getView().calculateExtent(map.getSize())
let dijonExtent = [561156.3584244443, 5998685.964885023, 561713.7014265041, 5998807.655496827];
map.getView().fit(dijonExtent);
};
let trackPosition = false;
let positionIconEl = document.getElementById('positionIcon');
document.getElementById('position').onclick = function() {
trackPosition = !trackPosition;
setGeolocationTracking(trackPosition);
positionIconEl.innerText = trackPosition ? '✓' : '✕';
};
document.getElementById('center-position').onclick = centerOnGeolocation;
// ------------------------------
// Connection status
// ------------------------------
window.addEventListener('online', updStatus);
window.addEventListener('offline', updStatus);
function updStatus() {
let statusSpan = document.getElementById('status');
if (statusSpan) {
statusSpan.innerText = navigator.onLine ? '⚡' : '💤';
}
}
updStatus();
// ------------------------------
// Map
// ------------------------------
const map = new Map({
target: 'map',
view: new View({
zoom: 5,
center: fromLonLat([5, 45]),
}),
});
const resolutions = [];
const matrixIds = [];
const proj3857 = getProjection('EPSG:3857');
const maxResolution = getWidth(proj3857.getExtent()) / 256;
for (let i = 0; i < 20; i++) {
matrixIds[i] = i.toString();
resolutions[i] = maxResolution / Math.pow(2, i);
}
const tileGrid = new WMTSTileGrid({
origin: [-20037508, 20037508],
resolutions: resolutions,
matrixIds: matrixIds,
});
const wmtsSource = new WMTS({
url: 'https://data.geopf.fr/wmts',
layer: 'ORTHOIMAGERY.ORTHOPHOTOS',
matrixSet: 'PM',
format: 'image/jpeg',
projection: 'EPSG:3857',
tileGrid: tileGrid,
style: 'normal',
attributions:
'<a href="https://www.ign.fr/" target="_blank">' +
'<img src="https://wxs.ign.fr/static/logos/IGN/IGN.gif" title="Institut national de l\'' +
'information géographique et forestière" alt="IGN"></a>',
// Lecture des tuiles online/offline
tileLoadFunction: onlineOrCacheTileLoadFunction
});
map.addLayer(new TileLayer({
source: wmtsSource,
}));
installGeolocation(map);
// Séparateur
const toolbar = document.getElementsByClassName('toolbar')[0];
toolbar.appendChild(document.createElement('br'));
// Cache sur l'étendue
installCache(map, wmtsSource);
const span = document.createElement('span');
span.textContent = ' | '
toolbar.appendChild(span);
// Cache sur dessin
installCacheIntersection(map, wmtsSource);