-
Notifications
You must be signed in to change notification settings - Fork 0
/
cities-many.html
232 lines (187 loc) · 6.36 KB
/
cities-many.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
222
223
224
225
226
227
228
229
230
231
232
<!DOCTYPE html>
<html>
<head>
<title>MapTiler Cities</title>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<!-- Import MapTiler SDK -->
<script src="https://cdn.maptiler.com/maptiler-sdk-js/v2.0.3/maptiler-sdk.umd.min.js"></script>
<link href="https://cdn.maptiler.com/maptiler-sdk-js/v2.0.3/maptiler-sdk.css" rel="stylesheet" />
<!-- Import MapTiler Maker Layout -->
<script src="../build/maptiler-marker-layout.umd.js"></script>
<style>
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}
ul {
list-style-type: none;
padding: 0;
margin: 5px;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
.marker {
position: absolute;
pointer-events: none;
color: #000;
line-height: 12px;
box-shadow: #00000040 0px 2px 10px;
}
.markerPointy {
width: 12px;
height: 12px;
background-color: #e6e6e6;
position: absolute;
transform: rotate(45deg) translate(-70%);
bottom: -12px;
left: 0;
right: -12px;
margin: auto;
box-shadow: #00000040 0px 2px 10px;
}
.markerBody {
position: absolute;
background: #e6e6e6;
width: 100%;
height: 100%;
top: 0;
border-radius: 2px;
/* padding: 3px; */
}
.markerTop {
border-top-left-radius: inherit;
border-top-right-radius: inherit;
width: 100%;
height: 20px;
line-height: 20px;
text-align: center;
background: #4d4d4d;
color: white;
font-size: 12px;
font-weight: 400;
}
.markerBottom {
border-radius: inherit;
width: 100%;
height: calc(100% - 20px);
line-height: 20px;
font-size: 12;
text-align: center;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.fade-in-animation {
animation: fadeIn 0.5s ease forwards;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
const appContainer = document.getElementById('map');
maptilersdk.config.apiKey = 'YOUR_API_KEY';
const map = new maptilersdk.Map({
container: appContainer,
hash: true,
geolocate: true,
});
// Creating the div that will contain all the markers
const markerContainer = document.createElement("div");
appContainer.appendChild(markerContainer);
(async () => {
await map.onReadyAsync();
const markerManager = new maptilermarkerlayout.MarkerLayout(map, {
layers: ["Capital city labels", "City labels", "Place labels", "Town labels"],
markerSize: [140, 40],
markerAnchor: "top",
offset: [0, -8], // so that the tip of the marker bottom pin lands on the city dot
sortingProperty: "rank",
// no filtering so that we get as many features as possible
});
// This object contains the marker DIV so that they can be updated rather than fully recreated every time
const markerLogicContainer = {};
let markerStatus = null;
// This function will be used as the callback for some map events
const updateMarkers = () => {
markerStatus = markerManager.update();
if (!markerStatus) return;
// Remove the div that corresponds to removed markers
markerStatus.removed.forEach((abstractMarker) => {
const markerDiv = markerLogicContainer[abstractMarker.id];
delete markerLogicContainer[abstractMarker.id];
markerContainer.removeChild(markerDiv);
});
// Update the div that corresponds to updated markers
markerStatus.updated.forEach((abstractMarker) => {
const markerDiv = markerLogicContainer[abstractMarker.id];
updateMarkerDiv(abstractMarker, markerDiv);
});
// Create the div that corresponds to the new markers
markerStatus.new.forEach((abstractMarker) => {
const markerDiv = makeMarker(abstractMarker);
markerLogicContainer[abstractMarker.id] = markerDiv;
markerContainer.appendChild(markerDiv);
});
}
const softUpdateMarkers = () => {
// A previous run of .update() yieding no result or not being ran at all
// would stop the soft update
if (!markerStatus) return;
markerStatus.updated.forEach((abstractMarker) => {
markerManager.softUpdateAbstractMarker(abstractMarker);
const markerDiv = markerLogicContainer[abstractMarker.id];
updateMarkerDiv(abstractMarker, markerDiv);
})
markerStatus.new.forEach((abstractMarker) => {
markerManager.softUpdateAbstractMarker(abstractMarker);
const markerDiv = markerLogicContainer[abstractMarker.id];
updateMarkerDiv(abstractMarker, markerDiv);
})
}
// While moving the map, this event is triggered many times per seconds
// so we only perform a soft update (that could be debounced)
map.on("move", softUpdateMarkers);
// When done moving, we perform a full update
map.on("moveend", updateMarkers)
// Full update at init
updateMarkers();
})()
function makeMarker(
abstractMarker,
) {
const marker = document.createElement("div");
marker.classList.add("marker");
marker.classList.add('fade-in-animation');
marker.style.setProperty("width", `${abstractMarker.size[0]}px`);
marker.style.setProperty("height", `${abstractMarker.size[1]}px`);
marker.style.setProperty("transform", `translate(${abstractMarker.position[0]}px, ${abstractMarker.position[1]}px)`);
const feature = abstractMarker.features[0];
marker.innerHTML = `
<div class="markerPointy"></div>
<div class="markerBody">
<div class="markerTop">
${feature.properties["name:en"] || feature.properties["name"]}
</div>
<div class="markerBottom">
<b>Rank:</b> ${feature.properties.rank}
</div>
</div>
`
return marker;
}
function updateMarkerDiv(abstractMarker, marker) {
marker.style.setProperty("width", `${abstractMarker.size[0]}px`);
marker.style.setProperty("height", `${abstractMarker.size[1]}px`);
marker.style.setProperty("transform", `translate(${abstractMarker.position[0]}px, ${abstractMarker.position[1]}px)`);
}
</script>
</body>
</html>