-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.html
584 lines (498 loc) · 14.5 KB
/
map.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Animate color visual variable [beta] - 4.5</title>
<link rel="stylesheet" href="https://js.arcgis.com/4.5/esri/themes/dark-blue/main.css">
<script>
var dojoConfig = {
has: {
// Enable webgl for feature layer in MapView
"esri-featurelayer-webgl": 1
}
};
</script>
<script type="text/javascript" src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://js.arcgis.com/4.5/"></script>
<style>
html,
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
background-color: #2b2d2f;
}
#applicationDiv {
position: absolute;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
overflow: hidden;
}
#viewDiv {
flex: 1 1 auto;
order: 1;
}
#titleDiv {
font-weight: 400;
font-style: normal;
font-size: 1.2019rem;
padding: 10px;
}
#sliderContainer {
flex: 0 0 80px;
order: 2;
display: flex;
flex-flow: row;
padding: 0 12px;
}
#sliderValue {
flex: 0 0 100px;
order: 1;
display: flex;
justify-content: center;
flex-direction: column;
text-align: center;
font-size: 16px;
}
#sliderInnerContainer {
flex: 1 1 auto;
order: 2;
display: flex;
flex-direction: column;
justify-content: center;
padding: 0 20px;
}
#sliderLabels {
flex: 1 1 auto;
order: 1;
display: flex;
justify-content: space-between;
margin-top: 20px;
}
#rangeWrapper {
flex: 1 1 auto;
order: 2;
position: relative;
padding: 0 20px;
}
#slider {
width: 100%;
}
/**
* Play/Stop toggle button
*/
#playButton {
flex: 0 0 100px;
order: 3;
margin: 20px 0;
}
.toggle-button {
display: flex;
}
.toggle-button.toggled .toggle-button-icon {
color: #CC1B1B;
}
.toggle-button .toggle-button-icon {
color: #1BCC1B;
}
.toggle-button> :nth-child(2) {
display: none;
}
.toggle-button.toggled> :nth-child(1) {
display: none;
}
.toggle-button.toggled> :nth-child(2) {
display: block;
}
/**
* Hover tooltip
*/
.tooltip {
position: absolute;
pointer-events: none;
transition: opacity 200ms;
}
.tooltip>div {
margin: 0 auto;
padding: 12px;
border-radius: 4px;
box-shadow: 0px 0px 4px rgba(255, 255, 255, 0.75);
transform: translate3d(-50%, -125%, 0);
}
</style>
<script>
require([
"esri/Map",
"esri/layers/FeatureLayer",
"esri/views/MapView",
"esri/widgets/Legend",
"esri/widgets/Home",
"dojo/domReady!"
], function(
Map,
FeatureLayer,
MapView,
Legend, Home
) {
//--------------------------------------------------------------------------
//
// Setup Map and View
//
//--------------------------------------------------------------------------
var layer = new FeatureLayer({
/*
portalItem: {
id: "38c9ed818c294cd8b7938465842e8150"
},
// definitionExpression: "CNSTRCT_YR > 0",
title: "Building Footprints",
minScale: 72223.819286
*/
url: "https://services8.arcgis.com/xzqcTVDn0tHsrBQe/arcgis/rest/services/travelers/FeatureServer",
// renderer: myRenderer,
});
var map = new Map({
basemap: {
portalItem: {
id: "4f2e99ba65e34bb8af49733d9778fb8e"
}
},
layers: [layer]
});
view = new MapView({
map: map,
container: "viewDiv",
center: [-45, 35],
zoom: 2,
/* constraints: {
snapToZoom: false,
minScale: 72223.819286
}, */
// This ensures that when going fullscreen
// The top left corner of the view extent
// stays aligned with the top left corner
// of the view's container
resizeAlign: "top-left"
});
//--------------------------------------------------------------------------
//
// Setup UI
//
//--------------------------------------------------------------------------
var applicationDiv = document.getElementById("applicationDiv");
var fullScreenButton = document.getElementById("fullScreenButton");
var slider = document.getElementById("slider");
var sliderValue = document.getElementById("sliderValue");
var playButton = document.getElementById("playButton");
var titleDiv = document.getElementById("titleDiv");
var animation = null;
// When user drags the slider:
// - stops the animation
// - set the visualized year to the slider one.
function inputHandler() {
stopAnimation();
setYear(parseInt(slider.value));
}
slider.addEventListener("input", inputHandler);
slider.addEventListener("change", inputHandler);
// Toggle animation on/off when user
// clicks on the play button
playButton.addEventListener("click", function() {
if (playButton.classList.contains("toggled")) {
stopAnimation();
}
else {
startAnimation();
}
});
view.ui.empty("top-left");
view.ui.add(titleDiv, "top-left");
view.ui.add(new Home({
view: view
}), "top-left");
view.ui.add(new Legend({
view: view
}), "bottom-left");
// When the layerview is available, setup hovering interactivity
// view.whenLayerView(layer).then(setupHoverTooltip);
// Starts the application by visualizing year 1984
setYear(0);
//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------
function setSliderValue(days) {
const sDate = moment.utc('2015-01-01').add(days, 'days').format('YYYY-MM-DD');
sliderValue.innerHTML = sDate;
}
/**
* Sets the current visualized construction year.
*/
function setYear(value) {
setSliderValue(value);
slider.value = Math.floor(value);
layer.renderer = createRenderer(value);
}
/**
* Returns a renderer with a color visual variable driven by the input
* year. The selected year will always render buildings built in that year
* with a light blue color. Buildings built 20+ years before the indicated
* year are visualized with a pink color. Buildings built within that
* 20-year time frame are assigned a color interpolated between blue and pink.
*/
function createRenderer(year) {
return {
type: 'simple',
symbol: {
type: 'simple-line',
width: 2,
color: '#0ff',
},
visualVariables: [
{
type: 'opacity',
field: 'dateValue',
stops:[
{ value: year - 40, opacity: 0 },
{ value: year, opacity: 0.5 },
{ value: year + 1, opacity: 0 },
],
},
/* {
type: 'color',
field: 'originLongitude',
stops:[
{ value: -180, color: '#f00' },
{ value: 0, color: '#0ff' },
{ value: 180, color: '#f00' },
],
}, */
]
};
}
/**
* Sets up a moving tooltip that displays
* the construction year of the hovered building.
*/
function setupHoverTooltip(layerview) {
var promise;
var highlight;
var tooltip = createTooltip();
view.on("pointer-move", function(event) {
if (promise) {
promise.cancel();
}
promise = view.hitTest(event.x, event.y)
.then(hit => {
promise = null;
// remove current highlighted feature
if (highlight) {
highlight.remove();
highlight = null;
}
// highlight the hovered feature
// or hide the tooltip
if (hit.results.length) {
var graphic = hit.results[0].graphic;
var screenPoint = hit.screenPoint;
highlight = layerview.highlight(graphic);
tooltip.show(screenPoint, "Built in " + graphic.getAttribute(
"CNSTRCT_YR"));
}
else {
tooltip.hide();
}
});
});
}
/**
* Starts the animation that cycle
* through the construction years.
*/
function startAnimation() {
stopAnimation();
animation = animate(parseFloat(slider.value));
playButton.classList.add("toggled");
}
/**
* Stops the animations
*/
function stopAnimation() {
if (!animation) {
return;
}
animation.remove();
animation = null;
playButton.classList.remove("toggled");
}
/**
* Animates the color visual variable continously
*/
function animate(startValue) {
var animating = true;
var value = startValue;
var frame = function(timestamp) {
if (!animating) {
return;
}
value += 1;
if (value > 1065) {
value = 0;
}
setYear(value);
// Update at 60fps
setTimeout(function() {
requestAnimationFrame(frame);
}, 1000 / 60);
}
frame();
return {
remove: function() {
animating = false;
}
};
}
/**
* Fullscreen API to easily move the application fullscreen
*/
var fullscreenAPI = null;
var fullScreenFnNames = ["requestFullscreen",
"webkitRequestFullscreen", "mozRequestFullScreen",
"msRequestFullscreen"
];
var fullscreenElementNames = ["fullscreenElement",
"webkitFullscreenElement", "mozFullScreenElement",
"msFullscreenElement"
];
var exitFullscreenFnNames = ["exitFullscreen", "webkitExitFullscreen",
"mozCancelFullScreen", "msExitFullscreen"
];
var fullScreenEventNames = ["fullscreenchange",
"webkitfullscreenchange", "mozfullscreenchange",
"MSFullscreenChange"
];
fullScreenFnNames.some(function(fnName, index) {
if (!view.container[fnName]) {
fullscreenAPI = {
supported: false
};
return false;
}
fullscreenAPI = {
supported: true,
eventName: fullScreenEventNames[index],
isFullscreen: function() {
return document[fullscreenElementNames[index]];
},
toggle: function() {
if (!document[fullscreenElementNames[index]]) {
applicationDiv[fnName]();
} else {
document[exitFullscreenFnNames[index]]();
}
}
}
return true;
});
if (!fullscreenAPI.supported) {
fullScreenButton.parentElement.removeChild(fullScreenButton);
}
else {
fullScreenButton.addEventListener("click", fullscreenAPI.toggle)
document.addEventListener(fullscreenAPI.eventName, function() {
if (fullscreenAPI.isFullscreen()) {
fullScreenButton.firstChild.className =
"esri-icon-zoom-in-fixed";
}
else {
fullScreenButton.firstChild.className =
"esri-icon-zoom-out-fixed";
}
});
view.ui.add(fullScreenButton, "top-right");
}
/**
* Creates a tooltip to display a the construction year of a building.
*/
function createTooltip() {
var tooltip = document.createElement("div");
var style = tooltip.style;
tooltip.setAttribute("role", "tooltip");
tooltip.classList.add("tooltip");
var textElement = document.createElement("div");
textElement.classList.add("esri-widget");
tooltip.appendChild(textElement);
view.container.appendChild(tooltip);
var x = 0;
var y = 0;
var targetX = 0;
var targetY = 0;
var visible = false;
// move the tooltip progressively
function move() {
x += (targetX - x) * 0.1;
y += (targetY - y) * 0.1;
if (Math.abs(targetX - x) < 1 && Math.abs(targetY - y) < 1) {
x = targetX;
y = targetY;
}
else {
requestAnimationFrame(move);
}
style.transform = "translate3d(" + Math.round(x) + "px," + Math.round(
y - view.height) + "px, 0)";
}
return {
show: function(point, text) {
if (!visible) {
x = point.x;
y = point.y;
}
targetX = point.x;
targetY = point.y;
style.opacity = 1;
visible = true;
textElement.innerHTML = text;
move();
},
hide: function() {
style.opacity = 0;
visible = false;
}
}
}
});
</script>
</head>
<body>
<div id="applicationDiv">
<div id="viewDiv">
<div id="titleDiv" class="esri-widget">World Travelers</div>
<div id="fullScreenButton" class="esri-widget esri-widget-button">
<span class="esri-icon-zoom-out-fixed" aria-label="fullscreen icon"></span>
</div>
</div>
<div id="sliderContainer" class="esri-widget">
<span id="sliderValue"></span>
<div id="sliderInnerContainer">
<div id="sliderLabels">
<span>0</span>
<span>1065</span>
</div>
<div id="rangeWrapper">
<input id="slider" type="range" min="0" max="1065" step="1" value="1984" />
</div>
</div>
<div id="playButton" class="esri-widget esri-widget-button toggle-button">
<div><span class="toggle-button-icon esri-icon-play" aria-label="play icon"></span> Play</div>
<div><span class="toggle-button-icon esri-icon-pause" aria-label="pause icon"></span> Pause</div>
</div>
</div>
</div>
</body>
</html>