-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
157 lines (129 loc) · 4.39 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.39.1/mapbox-gl.js'></script>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.39.1/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
h2, h3 {
margin: 10px;
font-size: 1.2em;
}
h3 {
font-size: 1em;
}
p {
font-size: 0.85em;
margin: 10px;
text-align: left;
}
/**
* Create a position for the map
* on the page */
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
/**
* Set rules for how the map overlays
* (info box and legend) will be displayed
* on the page. */
.map-overlay {
position: absolute;
bottom: 0;
right: 0;
background: rgba(255, 255, 255, 0.8);
margin-right: 20px;
font-family: Arial, sans-serif;
overflow: auto;
border-radius: 3px;
}
#features {
top: 0;
height: 100px;
margin-top: 20px;
width: 250px;
}
#legend {
padding: 10px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
line-height: 18px;
height: 200px;
margin-bottom: 40px;
width: 125px;
}
.legend-key {
display: inline-block;
border-radius: 20%;
width: 10px;
height: 10px;
margin-right: 5px;
}
</style>
</style>
</head>
<body>
<div id='map'></div>
<div class='map-overlay' id='features'><h2>Age of Burlington Buildings</h2><div id='pd'><p>Hover over a building!</p></div></div>
<div class='map-overlay' id='legend'>Legend</div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiZnJpbmdlZGdlbnRpYW4iLCJhIjoiY2o2NnAxOXBkMmZvcDJ3cTdhaDRmazE2dyJ9.RFzxwRRu5jeFggeuVzW12w';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/fringedgentian/cj6i96tyh546i2rnkozhmqj67', // stylesheet location
center: [-73.20, 44.48], // starting position [lng, lat]
zoom: 13.5 // starting zoom
});
map.on('load', function () {
//get the map style (as defined through mapbox interface)
var style = map.getStyle();
//get the one layer we are interested in, the one with building polygons with year built information. The name is visible in the mapbox interface.
var buildingsLayer = $.grep(style.layers, function(e){ return e.id == 'burlingtonbldgswyears-305ib6'; });
//get the segments as defined through the mapbox interface
var segments = buildingsLayer[0]["paint"]["fill-color"]["stops"];
//create legend based on segments set in mapbox interface
for (i = 0; i < segments.length; i++) {
var segment = segments[i];
var color = segment[1];
if (segments[i+1]) {
var year = segment[0] + ' to ' + (segments[i+1][0]-1);
} else {
var year = "over " + segment[0];
}
var item = document.createElement('div');
var key = document.createElement('span');
key.className = 'legend-key';
key.style.backgroundColor = color;
var value = document.createElement('span');
value.innerHTML = year;
item.appendChild(key);
item.appendChild(value);
legend.appendChild(item);
}
map.on('mousemove', function(e) {
var buildings = map.queryRenderedFeatures(e.point, {
layers: ['burlingtonbldgswyears-305ib6']
});
if (buildings.length > 0) {
document.getElementById('pd').innerHTML = 'This building was built in ' + buildings[0].properties.filtered_Y;
} else {
document.getElementById('pd').innerHTML = '<p>Hover over a building!</p>';
}
});
map.on('mouseenter', 'burlingtonbldgswyears-305ib6', function(e) {
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', 'burlingtonbldgswyears-305ib6', function() {
map.getCanvas().style.cursor = '';
});
});
</script>
</body>
</html>