/
maplibre-style-switcher.html
221 lines (212 loc) · 8.89 KB
/
maplibre-style-switcher.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<html>
<head>
<title>MapLibre style switcher</title>
<meta charset="UTF-8" />
<script src="../.env"></script>
<script src="https://unpkg.com/maplibre-gl@2.1.6/dist/maplibre-gl.js"></script>
<link
href="https://unpkg.com/maplibre-gl@2.1.6/dist/maplibre-gl.css"
rel="stylesheet"
/>
<script>
class MapLibreStyleSwitcherControl {
constructor(styles, defaultStyle) {
this.styles = styles || MapLibreStyleSwitcherControl.DEFAULT_STYLES;
this.defaultStyle =
defaultStyle || MapLibreStyleSwitcherControl.DEFAULT_STYLE;
this.onDocumentClick = this.onDocumentClick.bind(this);
}
getDefaultPosition() {
const defaultPosition = "top-right";
return defaultPosition;
}
onAdd(map) {
this.map = map;
this.controlContainer = document.createElement("div");
this.controlContainer.classList.add("maplibregl-ctrl");
this.controlContainer.classList.add("maplibregl-ctrl-group");
this.mapStyleContainer = document.createElement("div");
this.styleButton = document.createElement("button");
this.styleButton.type = "button";
this.mapStyleContainer.classList.add("maplibregl-style-list");
for (const style of this.styles) {
const styleElement = document.createElement("button");
styleElement.type = "button";
styleElement.innerText = style.title;
styleElement.classList.add(
style.title.replace(/[^a-z0-9-]/gi, "_")
);
styleElement.dataset.uri = JSON.stringify(style.uri);
styleElement.addEventListener("click", (event) => {
const srcElement = event.srcElement;
if (srcElement.classList.contains("active")) {
return;
}
this.map.setStyle(JSON.parse(srcElement.dataset.uri));
this.mapStyleContainer.style.display = "none";
this.styleButton.style.display = "block";
const elms =
this.mapStyleContainer.getElementsByClassName("active");
while (elms[0]) {
elms[0].classList.remove("active");
}
srcElement.classList.add("active");
});
if (style.title === this.defaultStyle) {
styleElement.classList.add("active");
}
this.mapStyleContainer.appendChild(styleElement);
}
this.styleButton.classList.add("maplibregl-ctrl-icon");
this.styleButton.classList.add("maplibregl-style-switcher");
this.styleButton.addEventListener("click", () => {
this.styleButton.style.display = "none";
this.mapStyleContainer.style.display = "block";
});
document.addEventListener("click", this.onDocumentClick);
this.controlContainer.appendChild(this.styleButton);
this.controlContainer.appendChild(this.mapStyleContainer);
return this.controlContainer;
}
onRemove() {
if (
!this.controlContainer ||
!this.controlContainer.parentNode ||
!this.map ||
!this.styleButton
) {
return;
}
this.styleButton.removeEventListener("click", this.onDocumentClick);
this.controlContainer.parentNode.removeChild(this.controlContainer);
document.removeEventListener("click", this.onDocumentClick);
this.map = undefined;
}
onDocumentClick(event) {
if (
this.controlContainer &&
!this.controlContainer.contains(event.target) &&
this.mapStyleContainer &&
this.styleButton
) {
this.mapStyleContainer.style.display = "none";
this.styleButton.style.display = "block";
}
}
}
// https://cloud.maptiler.com/maps/
// https://github.com/maplibre/demotiles
MapLibreStyleSwitcherControl.DEFAULT_STYLE = "Demotiles";
MapLibreStyleSwitcherControl.DEFAULT_STYLES = [
{
title: "Demotiles",
uri: "https://demotiles.maplibre.org/style.json",
},
{
title: "Baisc",
uri:
"https://api.maptiler.com/maps/basic/style.json?key=" +
config.MAPTILER_TOKEN,
},
{
title: "Light",
uri:
"https://api.maptiler.com/maps/bright/style.json?key=" +
config.MAPTILER_TOKEN,
},
{
title: "Outdoors",
uri:
"https://api.maptiler.com/maps/outdoor/style.json?key=" +
config.MAPTILER_TOKEN,
},
{
title: "Satellite Hybrid",
uri:
"https://api.maptiler.com/maps/hybrid/style.json?key=" +
config.MAPTILER_TOKEN,
},
{
title: "Streets",
uri:
"https://api.maptiler.com/maps/streets/style.json?key=" +
config.MAPTILER_TOKEN,
},
];
</script>
<style>
.maplibregl-style-list {
display: none;
}
.maplibregl-ctrl-group .maplibregl-style-list button {
background: none;
border: none;
cursor: pointer;
display: block;
font-size: 14px;
padding: 8px 8px 6px;
text-align: right;
width: 100%;
height: auto;
}
.maplibregl-style-list button.active {
font-weight: bold;
}
.maplibregl-style-list button:hover {
background-color: rgba(0, 0, 0, 0.05);
}
.maplibregl-style-list button + button {
border-top: 1px solid #ddd;
}
.maplibregl-style-switcher {
background-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PHN2ZyB2ZXJzaW9uPSIxLjEiIGlkPSJDYXBhXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB3aWR0aD0iNTQuODQ5cHgiIGhlaWdodD0iNTQuODQ5cHgiIHZpZXdCb3g9IjAgMCA1NC44NDkgNTQuODQ5IiBzdHlsZT0iZW5hYmxlLWJhY2tncm91bmQ6bmV3IDAgMCA1NC44NDkgNTQuODQ5OyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+PGc+PGc+PGc+PHBhdGggZD0iTTU0LjQ5NywzOS42MTRsLTEwLjM2My00LjQ5bC0xNC45MTcsN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2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjwvc3ZnPg==);
background-position: center;
background-repeat: no-repeat;
background-size: 70%;
}
</style>
</head>
<body>
<h2 style="font-family: sans-serif">MapLibre style switcher</h2>
<div id="map" style="height: 60%"></div>
<script>
const map = new maplibregl.Map({
container: "map",
style: "https://demotiles.maplibre.org/style.json",
center: [-122.4194, 37.7788],
zoom: 12,
});
var styles = (MapLibreStyleDefinition = [
{
title: "Demotiles",
uri: "https://demotiles.maplibre.org/style.json",
},
{
title: "Basic",
uri:
"https://api.maptiler.com/maps/streets/style.json?key=" +
config.MAPTILER_TOKEN,
},
{
title: "Light",
uri:
"https://api.maptiler.com/maps/bright/style.json?key=" +
config.MAPTILER_TOKEN,
},
{
title: "Satellite Hybrid",
uri:
"https://api.maptiler.com/maps/hybrid/style.json?key=" +
config.MAPTILER_TOKEN,
},
{
title: "Outdoors",
uri:
"https://api.maptiler.com/maps/outdoor/style.json?key=" +
config.MAPTILER_TOKEN,
},
]);
map.addControl(new MapLibreStyleSwitcherControl(styles));
</script>
</body>
</html>