-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenmaptiles.js
82 lines (78 loc) · 2.67 KB
/
openmaptiles.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
async function run () {
const map = L.map('map', {
center: [47, 2],
zoom: 6,
zoomControl: false,
fullscreenControl: true
})
const fontsubmap = {
'Inter': { face: 'Inter' },
'Raleway': { face: 'Raleway' },
'Work Sans': { face: 'Work Sans' },
'Petrona': { face: 'Petrona' },
'Manrope': { face: 'Manrope' }
}
/*
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map)
*/
const response = await fetch('http://127.0.0.1:8080/osm-bright.json')
if (response.status !== 200) {
throw new Error(`Impossible to fetch style: ` + response.status)
}
const style = await response.json()
const backgroundLayer = style.layers.find(layer => layer.type === 'background')
const backgroundColor = backgroundLayer.paint['fill-color']
// Reflect default language/font value into style
updateStyleFont()
updateStyleLanguage()
const rules = pmtiles.mapbox_style(style, fontsubmap)
const layer = protomapsL.leafletLayer({
url: 'http://127.0.0.1:8081/planet-02-08-2023.pmtiles',
...rules,
tasks:[document.fonts.load('12px Work Sans'),document.fonts.load('12px Manrope'),document.fonts.load('12px Inter'),document.fonts.load('12px Petrona'),document.fonts.load('12px Raleway')],
//debug: true,
//levelDiff: 2,
backgroundColor,
maxDataZoom: 14,
maxNativeZoom: 18
}).addTo(map)
function updateStyleFont() {
style.layers.forEach(layer => {
if (layer.layout && layer.layout['text-font']) {
layer.layout['text-font'] = [document.getElementById('selectedFont').value]
}
})
}
function updateStyleLanguage() {
// Default value
let field = 'name'
if (document.getElementById('selectedLanguage').value == 'English') field = 'name:en'
else if (document.getElementById('selectedLanguage').value == 'French') field = 'name:fr'
else if (document.getElementById('selectedLanguage').value == 'Latin') field = 'name:latin'
style.layers.forEach(layer => {
if (layer.layout && layer.layout['text-field']) {
layer.layout['text-field'] = field
}
})
}
function updateStyle() {
const rules = pmtiles.mapbox_style(style, fontsubmap)
Object.assign(layer, rules)
layer.clearLayout()
layer.rerenderTiles()
}
function updateFont() {
updateStyleFont()
updateStyle()
}
function updateLanguage() {
updateStyleLanguage()
updateStyle()
}
document.getElementById('selectedFont').addEventListener('change', updateFont)
document.getElementById('selectedLanguage').addEventListener('change', updateLanguage)
}
run()