forked from DCgov/opendatadc-starterkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcopendata_income_choropleth_google.html
416 lines (370 loc) · 14.1 KB
/
dcopendata_income_choropleth_google.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
<!DOCTYPE html>
<html>
<head>
<!-- Description: This sample demonstrates how to create a colorful interactive choropleth map of census tracts using data from DC Open Data with the help of Google Map JS API -->
<!-- Attribution:
Google Map JS API Code Samples, Url: https://developers.google.com/maps/documentation/javascript/examples/;
Google Fusion Tables REST API Example: Url: https://developers.google.com/fusiontables/docs/samples/dynamic_styling_template;
Daniel Margol, How to create a choropleth map with GeoJSON and Google Maps, Url: https://blog.vizuri.com/how-to-create-a-choropleth-map-with-geojson-and-google-maps.
Styling inspired by Esri Story Map: https://storymaps.esri.com/stories/2016/top-stories/
Snazzy Maps: https://snazzymaps.com/
-->
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Census Tracts by Median Income @ Washington, DC</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?client=gme-docg&v=3.26&channel=dcopendatasamples&callback=initMap" async defer></script>
<link rel="shortcut icon" type="image/x-icon" href="https://dc.gov/sites/default/files/favicon_0.ico">
<style>
body {
margin: 0;
padding: 0;
font-family: "open_sanssemi", sans-serif;
}
#map-canvas {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
#legend {
background-color: #FFF;
margin: 10px;
padding: 5px;
width: 200px;
}
#legend p {
font-weight: bold;
margin-top: 3px;
font-size: 14px;
}
#legend div {
clear: both;
}
#legend div span {
font-size: 14px;
}
.color {
height: 24px;
width: 58px;
margin-right: 10px;
float: left;
display: block;
}
a {
color: #fff;
font-family: 'open_sansregular', sans-serif;
}
</style>
<style>
/*styling for title bar and about pop over */
#title {
color: #FFF;
font-family: "open_sanssemibold", sans-serif;
padding: 3 0 0 4;
font-size: 24px;
line-height: 30px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.header {
position: relative;
padding: 15px 30px 25px 30px;
margin: auto;
z-index: 500;
background-color: rgb(12, 12, 12);
box-shadow: 0 3px 5px 0 rgba(0, 0, 0, .75);
opacity: 0.8;
}
#about {
padding: 10px 20px 20px 20px;
z-index: 4000;
position: absolute;
background-color: #505050;
color: white;
width: 700px;
left: 50%;
margin-left: -300px;
height: calc(100% - 200px);
top: 100px;
display: none;
overflow: hidden;
opacity: 0.95;
}
#dismiss-about {
cursor: pointer;
font-size: 30px;
float: right;
margin-bottom: 10px;
}
#inner {
width: 100%;
height: calc(100% - 60px);
overflow-y: auto;
-webkit-overflow-scrolling: touch;
}
</style>
<script>
var map;
//Data source from DC Open Data
var censusGeoJsonUrl = "https://opendata.arcgis.com/datasets/6969dd63c5cb4d6aa32f15effb8311f3_8.geojson";
var colorOpacity=0.75;
var layerStyles = [{
'min': 0,
'max': 20000,
'color': '#ffcccc'
}, {
'min': 20000,
'max': 30000,
'color': '#ff9999'
}, {
'min': 30000,
'max': 50000,
'color': '#ff6666'
}, {
'min': 50000,
'max': 80000,
'color': '#ff3232'
}, {
'min': 80000,
'max': 100000,
'color': '#ff0000'
}, {
'min': 100000,
'max': 'plus',
'color': '#800000'
}];
function initMap() {
var latlngDC = new google.maps.LatLng(38.8951, -77.0367); // Coordinates of Washington, DC (area centroid)
// Set the Map Options to be applied when the map is set.
var mapOptions = {
zoom: 12,
scrollwheel: true,
center: latlngDC,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.TERRAIN]
},
streetViewControl: false, //Hide Pegman/street view
zoomControlOptions: {
position: google.maps.ControlPosition.LEFT_BOTTOM
},
styles:
[
{
"featureType": "administrative",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#444444"
}
]
},
{
"featureType": "landscape",
"elementType": "all",
"stylers": [
{
"color": "#f2f2f2"
}
]
},
{
"featureType": "poi",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road",
"elementType": "all",
"stylers": [
{
"saturation": -100
},
{
"lightness": 45
}
]
},
{
"featureType": "road.highway",
"elementType": "all",
"stylers": [
{
"visibility": "simplified"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "transit",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "water",
"elementType": "all",
"stylers": [
{
"color": "#46bcec"
},
{
"visibility": "on"
}
]
}
]
}
// Set a blank infoWindow to be used for each census tract on click
var infoWindow = new google.maps.InfoWindow({
content: ""
});
// Set the map to the element ID and give it the map options to be applied
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
// Create the census tract data layer and load the GeoJson Data
var censusLayer = new google.maps.Data();
censusLayer.loadGeoJson(censusGeoJsonUrl);
// Set and apply styling to the censusLayer
censusLayer.setStyle(function(feature) {
return {
fillColor: getColor(feature.getProperty('FAGI_MEDIAN_2010')), // call function to get color for census tract based on the median income
fillOpacity: colorOpacity,
strokeColor: '#fff',
strokeWeight: 1,
zIndex: 1
};
});
// Add mouseover and mouse out styling for the GeoJSON census tract data
censusLayer.addListener('mouseover', function(e) {
censusLayer.overrideStyle(e.feature, {
strokeColor: '#F01141',
strokeWeight: 4,
zIndex: 2
});
});
censusLayer.addListener('mouseout', function(e) {
censusLayer.revertStyle();
});
// Adds mouseover handler
censusLayer.addListener('mouseover', function(e) {
infoWindow.setContent('<div style="line-height:1.00;overflow:hidden;white-space:nowrap;">' + '<b>Census Tract: ' +
e.feature.getProperty('TRACT') + '</b><hr><br> Population: ' +
e.feature.getProperty('P0010001') + '<br> Area (Sq. Mi.): ' +
e.feature.getProperty('SQ_MILES') + '<br> 2010 Median Income: $' +
e.feature.getProperty('FAGI_MEDIAN_2010') + '</div>');
var anchor = new google.maps.MVCObject();
anchor.set("position", e.latLng);
infoWindow.open(map, anchor);
});
// Set the censusLayer GeoJSON data onto the map
censusLayer.setMap(map);
// returns a color based on the value given when the function is called
function getColor(FAGI_MEDIAN_2010) {
var colors = layerStyles.map(function(item) {
return item.color;
});
return FAGI_MEDIAN_2010 >= 100000 ? colors[5] :
FAGI_MEDIAN_2010 > 80000 ? colors[4] :
FAGI_MEDIAN_2010 > 50000 ? colors[3] :
FAGI_MEDIAN_2010 > 30000 ? colors[2] :
FAGI_MEDIAN_2010 > 20000 ? colors[1] :
colors[0];
}
addLegend(map);
}
// Initialize the legend
function addLegend(map) {
var legendWrapper = document.createElement('div');
legendWrapper.id = 'legendWrapper';
legendWrapper.index = 1;
map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(
legendWrapper);
legendContent(legendWrapper);
}
// Generate the content for the legend
function legendContent(legendWrapper) {
var legend = document.createElement('div');
legend.id = 'legend';
var title = document.createElement('p');
title.innerHTML = "Family Median Income";
legend.appendChild(title);
for (var i in layerStyles) {
var style = layerStyles[i];
var legendItem = document.createElement('div');
var color = document.createElement('span');
color.setAttribute('class', 'color');
color.style.backgroundColor = style.color;
color.style.opacity=colorOpacity;
legendItem.appendChild(color);
var minMax = document.createElement('span');
minMax.innerHTML = style.min + ' - ' + style.max;
legendItem.appendChild(minMax);
legend.appendChild(legendItem);
}
legendWrapper.appendChild(legend);
}
//open About This panel
function openAbout() {
$("#about").show();
return false;
}
//close About This panel
function closeAbout() {
$("#about").hide();
return false;
}
</script>
</head>
<body>
<div id="map-canvas">
</div>
<div class='header'>
<div id="title">Census Tracts by Median Income @ Washington, DC</div>
<div id="sourceUrls" style="float:right">
<a style="color:rgb(206,206,206); font-size:14px;margin-right: 10px;" href="https://github.com/DCgov/opendatadc-starterkit" target="_blank">This on Github</a>
<a style="color:rgb(206,206,206);font-size:14px; " href="javascript:void(0)" onclick="openAbout()">About This Map</a>
</div>
</div>
<div id="about">
<a id="dismiss-about" onclick="closeAbout()">×</a>
<div id="inner">
<p style="font-size: 1.4em; text-transform: uppercase; text-align: center">About this map
</p>
<p style="font-size: 1.2em;">This sample demonstrates how to create a colorful interactive choropleth map of census tracts using data from DC Open Data with the help of Google Map JS API.</p>
<p><strong>Instructions</strong>
<br/> Mouse over on census tracts for more information.</p>
<p><strong>Built with...</strong>
<br/> <a href="https://developers.google.com/maps/documentation/javascript/" target="_blank">Google Maps JS API</a>, <a href="https://jquery.com/" target="_blank">JQuery</a></p>
<p><strong>Download Census Tracts - 2010 data in GeoJSON</strong>
<br/><a href="https://opendata.arcgis.com/datasets/6969dd63c5cb4d6aa32f15effb8311f3_8.geojson" target="_blank">https://opendata.arcgis.com/datasets/6969dd63c5cb4d6aa32f15effb8311f3_8.geojson</a></p>
<p><strong>View Census Tracts - 2010 data on DC Open Data</strong>
<br/><a href="http://opendata.dc.gov/datasets/census-tracts-2010" target="_blank">https://opendata.arcgis.com/datasets/6969dd63c5cb4d6aa32f15effb8311f3_8</a></p>
<p><strong>DC Open Data</strong>
<br/><a href="http://opendata.dc.gov" target="_blank">http://opendata.dc.gov</a></p>
<p><strong>DC Open Data Samples</strong>
<br/><a href="https://github.com/DCgov/opendatadc-starterkit" target="_blank">https://github.com/DCgov/opendatadc-starterkit</a></p>
<div id="logos" style="text-align: center">
<img src="images\WeAreWashintgonDCFinalLogo-Website.png" title="We are DC logo">
</div>
</div>
</div>
</body>
</html>