-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
68 lines (58 loc) · 1.82 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>OS Labels</title>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel='stylesheet' href='https://unpkg.com/maplibre-gl@3.3.0/dist/maplibre-gl.css' />
<script src='https://unpkg.com/maplibre-gl@3.3.0/dist/maplibre-gl.js'></script>
<style>
body {
margin: 0;
padding: 0;
}
html, body, #map {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
const map = new maplibregl.Map({
style: "https://api.maptiler.com/maps/streets/style.json?key=KVZVsqgpG5DhG9pfsOIx",
center: [-3.1, 51.25],
zoom: 10,
container: "map"
});
map.on('load', function() {
map.addSource("labels", {
"type": "vector",
"tiles": [ location.origin + location.pathname + "labels/{z}/{x}/{y}.pbf" ],
"maxzoom": 14
});
map.addLayer({
"id": "labels-fill",
"type": "fill",
"source": "labels",
"source-layer": "oslabels",
"paint": {
"fill-color": "#ff0000"
}
});
});
map.on('click', 'labels-fill', e => {
const coordinates = e.features[0].geometry.coordinates.slice()[0];
const sum = arr => arr.reduce((sum, val) => sum + val, 0);
const centroidX = sum(coordinates.map(t => t[0])) / coordinates.length;
const centroidY = sum(coordinates.map(t => t[1])) / coordinates.length;
const { text, score, postocr_label } = e.features[0].properties;
new maplibregl.Popup()
.setLngLat([centroidX, centroidY])
.setHTML(text)
.addTo(map);
});
</script>
</body>
</html>