-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathdynamic-label.html
More file actions
371 lines (311 loc) · 10.2 KB
/
dynamic-label.html
File metadata and controls
371 lines (311 loc) · 10.2 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<html>
<head>
<title>Dynamic Label</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../lib/Leaflet/leaflet-0.7.3/leaflet.css"/>
<link rel="stylesheet" href="css/LayerList.css"/>
<link rel="stylesheet" href="../src/DynamicLabel/label.css"/>
<style>
body {
margin: 0;
padding: 0;
font: 12px 'Helvetica Neue', Arial, Helvetica, sans-serif;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
#style-ui {
position: absolute;
top: 100px;
left: 100px;
height: 300px;
width: 300px;
z-index: 1;
background-color: #fff;
display: none;
}
/* info block */
.rotate {
-webkit-transform: rotate(270deg);
-moz-transform: rotate(270deg);
-ms-transform: rotate(270deg);
-o-transform: rotate(270deg);
transform: rotate(270deg);
-webkit-transform-origin: 50% 50%;
-moz-transform-origin: 50% 50%;
-ms-transform-origin: 50% 50%;
-o-transform-origin: 50% 50%;
transform-origin: 50% 50%;
}
.stack {
margin: 20px -8px;
}
.trigger {
z-index: 2000;
position: absolute;
right: 0px;
top: 50%;
margin-top: -54px;
-moz-border-radius: 5px 0 0 5px;
-webkit-border-radius: 5px 0 0 5px;
border-radius: 5px 0 0 5px;
cursor: pointer;
background-color: white;
text-transform: uppercase;
}
.block {
z-index: 2000;
position: absolute;
right: -300px;
width: 270px;
height: 100%;
background-color: white;
padding: 0 15px;
overflow-y: auto;
}
.input-label {
margin-top: 15px;
}
.round-it {
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
</style>
</head>
<body>
<div id="map" class="leaflet-container leaflet-fade-anim" tabindex="0"></div>
<div class="block">
<div class="input-label">Vector Tiles Style:</div>
<input id="cssFill" type="text" placeholder="#523422"/>
<input id="cssStroke" type="text" placeholder="#ff3322"/>
<input type="button" value="Update" onclick="updateStyle()"/>
<br>
<br>
<br>
<input type="button" value="Update Layer list" onclick="loadLayers()"/>
<div id="layerList">
</div>
<br>
<br>
<br>
<div id="idResults">
</div>
<input type="button" value="Remove PBFLayer" onclick="removePBFLayer()"/>
</div>
<div class="trigger">
<div class="stack rotate">
style
</div>
</div>
<script src="../lib/Leaflet/leaflet-0.7.3/leaflet-src.js"></script>
<script src="../lib/vectortile/vectortilelayer.js"></script>
<script src="../lib/vectortile/vectortilefeature.js"></script>
<script src="../lib/vectortile/vectortile.js"></script>
<script src="../lib/vectortile/pbf/ieee/ieee.js"></script>
<script src="../lib/vectortile/pbf/pbf.js"></script>
<script src="../lib/vectortile/point.js"></script>
<script src="../lib/jQuery/jQuery-2.1.1.min.js"></script>
<script src="../lib/Buffer/browser-buffer.min.js"></script>
<script src="scripts/Leaflet.PBFLayer/TileLayer.PBFSource.js"></script>
<script src="scripts/Leaflet.PBFLayer/TileLayer.PBFLayer.js"></script>
<script src="../src/PBFFeature.js"></script>
<script src="../src/DynamicLabel/Feature.js"></script>
<script src="../src/DynamicLabel/DynamicLabel.js"></script>
<script>
var debug = {};
// panel
var open = 0;
$(".trigger").click(function() {
if (open == 0) {
//$('#start-input').focus();
$(".trigger").animate({ "right": "+=300px" }, "fast");
$(".block").animate({ "right": "+=300px" }, "fast", function() {
$('#start-input').focus();
});
open = 1;
}
else if (open == 1) {
$(".block").animate({ "right": "-=300px" }, "fast");
$(".trigger").animate({ "right": "-=300px" }, "fast");
open = 0;
}
});
var map = L.map('map').setView([-5,27.4], 5); // africa
// var map = L.map('map').setView([19.6,-155.4], 8); // hawaii
L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
id: 'examples.map-i86knfo3'
}).addTo(map);
var pbfSource = new L.TileLayer.MVTSource({
url: "http://localhost:3001/services/vector-tiles/GADM/{z}/{x}/{y}.pbf",
// url: "https://a.tiles.mapbox.com/v3/mapbox.mapbox-terrain-v1,mapbox.mapbox-streets-v5/{z}/{x}/{y}.vector.pbf",
debug: true,
clickableLayers: ["gadm0"],
getIDForLayerFeature: function(feature) {
switch (feature.layer.name) {
case "gadm0":
return feature.properties.id_0;
case "gadm1":
return feature.properties.id_0 + "-" + feature.properties.id_1;
case "gadm2":
return feature.properties.id_0 + "-" + feature.properties.id_1 + "-" + feature.properties.id_2;
}
return "";
},
/**
* The filter function gets called when iterating though each vector tile feature (vtf). You have access
* to every property associated with a given feature (the feature, and the layer). You can also filter
* based of the context (each tile that the feature is drawn onto).
*
* Returning false skips over the feature and it is not drawn.
*
* @param feature
* @returns {boolean}
*/
filter: function(feature, context) {
if (feature.layer.name === 'gadm0') {
return true;
}
// if (feature.layer.name === 'gadm0' && (context.id === '5:17:16' || context.id === '5:18:16')) { // Democratic Republic of the Congo
// if (feature.properties.id_0 === 62) {
// return true;
// }
// }
return false;
}
});
debug.mvtSource = pbfSource;
//Globals that we can change later.
var fillColor = 'rgba(149,139,255,0.4)';
var strokeColor = 'rgb(20,20,20)';
pbfSource.style = pbfStyle;
function pbfStyle(feature) {
var style = {};
var type = feature.type;
switch (type) {
case 1: //'Point'
style.color = 'rgba(252,146,114,0.6)';
style.radius = 5;
case 2: //'LineString'
style.color = 'rgba(161,217,155,0.8)';
style.size = 3;
case 3: //'Polygon'
style.color = fillColor;
style.outline = {
color: strokeColor,
size: 2
};
}
if (feature.layer.name === 'gadm0') {
style.dynamicLabel = function() {
var style = {
html: feature.properties.name_0,
iconSize: [100,100],
cssClass: 'label-icon-text'
};
return style;
};
}
return style;
}
function pbfSelectedStyle(vectorTileFeature) {
var type = vectorTileFeature.type;
switch (type) {
case 1: //'Point'
return {
color: 'rgba(255,255,0,0.5)',
radius: 5
};
break;
case 2: //'LineString'
return {
color: 'rgba(255,25,0,0.5)',
size: 3
};
break;
case 3: //'Polygon'
return {
color: 'rgba(255,140,0,0.3)',
outline: {
color: 'rgba(255,140,0,1)',
size: 1
}
};
break;
default:
return null;
}
}
map.on("click", function(e) {
//Take the click event and pass it to the group layers.
pbfSource._onClick(e, function (evt) {
if (evt && evt.feature) {
//alert("Clicked Country: " + evt.feature.name_0);
$("#idResults").html("Clicked Country: " + evt.feature.properties.name_0);
evt.feature.isSelected = !evt.feature.isSelected;
var style;
if (evt.feature.isSelected === true) {
//Set selected style
style = pbfSelectedStyle(evt.feature);
}
else {
//return to normal color
style = pbfStyle(evt.feature);
}
//set it - this triggers an auto tile redraw
evt.feature.setStyle(style);
}
});
});
map.on("layerremove", function(removed){
//This is the layer that was removed.
//If it is a TileLayer.MVTSource, then call a method to actually remove the children, too.
if(removed.layer.removeChildLayers){
removed.layer.removeChildLayers(map);
}
});
//Add layer
map.addLayer(pbfSource);
function loadLayers() {
//Load layer list
var layers = pbfSource.getLayers();
var layerIds = Object.keys(layers);
layerIds.forEach(function(key, idx){
//var key = layerIds[idx];
//loop thru layers and list them in the panel
var layer = layers[key];
var row = $('<div class="VTSubLayer"><label class="checkbox">' + key + '</label></div>').appendTo($("#layerList"));
var cBox = $('<input type="checkbox" checked="checked" value="all">').appendTo(row);
cBox.on("click", function(evt) {
//Toggle layer visiblity
if (layer.visible == true) {
pbfSource.hideLayer(key);
}
else {
pbfSource.showLayer(key);
pbfSource.redraw();
}
});
})
}
function removePBFLayer(){
map.removeLayer(pbfSource);
}
function updateStyle() {
var cssFill = $("#cssFill").val();
var cssStroke = $("#cssStroke").val();
if (cssFill) fillColor = cssFill;
if (cssStroke) strokeColor = cssStroke;
pbfLayerGroup.redraw();
}
</script>
</body>
</html>