-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombineAndVisualizeMultipleData.html
More file actions
302 lines (276 loc) · 13 KB
/
CombineAndVisualizeMultipleData.html
File metadata and controls
302 lines (276 loc) · 13 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
<!--
#########
Combine and Visualize Multiple Data Sources
#########
This section shows you to display data from multiple sources on Google map. For example,
the choropleth maps here uses two different sources to highlight various US states and
display state-specific data.
The map uses data from GeoJSON file to display polygons that define US state boundaries.
It can also present data on the map corresponding to each state, which comes from a
simulated query to the US Census API.
Select a category of data from the control dropdown menu to update the polygons on the map.
You can also hover over a state polygon to view state-specific information in a data
box control on the map.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Combine and Visualize Multiple Data Sources on Google Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="UTF-8">
<style type="text/css">
html, body, #map {
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
.nicebox {
position: absolute;
text-align: center;
font-family: 'Roboto', 'Arial', sans-serif;
font-size: 13px;
z-index: 5;
box-shadow: 0 4px 6px -4px #333333;
padding: 5px 10px;
background: rgb(255, 255, 255);
background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(245, 245, 245, 1) 100%);
border: rgb(229, 229, 229) 1px solid;
}
#controls {
top: 10px;
left: 110px;
width: 360px;
height: 45px;
}
#data-box {
top: 10px;
left: 500px;
height: 45px;
line-height: 45px;
display: none;
}
#census-variable {
width: 360px;
height: 20px;
}
#legend {
display: flex;
display: -webkit-box;
padding-top: 7px;
}
.color-key {
background: linear-gradient(to right,
hsl(5, 69%, 54%) 0%,
hsl(29, 71%, 51%) 17%,
hsl(54, 74%, 47%) 33%,
hsl(78, 76%, 44%) 50%,
hsl(102, 78%, 41%) 67%,
hsl(127, 81%, 37%) 83%,
hsl(151, 83%, 34%) 100%);
flex: 1;
-webkit-box-flex: 1;
margin: 0 5px;
text-align: left;
font-size: 1.0em;
line-height: 1.0em;
}
#data-value {
font-size: 2.0em;
font-weight: bold;
}
#data-label {
font-size: 2.0em;
font-weight: normal;
padding-right: 10px;
}
#data-label:after {
content: ':';
}
#data-caret {
margin-left: -5px;
display: none;
font-size: 14px;
width: 14px;
}
</style>
</head>
<body>
<div id="controls" class="nicebox">
<div>
<!-- A control with a dropdown menu that has 5 different data options -->
<select id="census-variable">
<option value="https://storage.googleapis.com/mapsdevsite/json/DP02_0066PE">Percent of population over 25 that completed high school</option>
<option value="https://storage.googleapis.com/mapsdevsite/json/DP05_0017E">Median Age</option>
<option value="https://storage.googleapis.com/mapsdevsite/json/DP05_0001E">Total Population</option>
<option value="https://storage.googleapis.com/mapsdevsite/json/DP02_0016E">Average Family Size</option>
<option value="https://storage.googleapis.com/mapsdevsite/json/DP03_0088E">Per-Capita Income</option>
</select>
</div>
<!-- A map legend -->
<div id="legend">
<div id="census-min">min</div>
<div class="color-key"><span id="data-caret">◆</span></div>
<div id="census-max">max</div>
</div>
</div>
<!-- A data box displaying state-specific data which appears when you hover over a polygon -->
<div id="data-box" class="nicebox">
<label id="data-label" for="data-value"></label>
<span id="data-value"></span>
</div>
<div id="map"></div>
<script type="text/javascript">
/*
The stylers in the code below turn off the visibility of all "featureTypes" on the
map like roads, points of interest, landscape, administrative areas, and all their
"elementTypes".
*/
var mapStyle = [{
'featureType': 'all',
'elementType': 'all',
'stylers': [{'visibility': 'off'}]
}, {
'featureType': 'landscape',
'elementType': 'geometry',
'stylers': [{'visibility': 'on'}, {'color': '#fcfcfc'}]
}, {
'featureType': 'water',
'elementType': 'labels',
'stylers': [{'visibility': 'off'}]
}, {
'featureType': 'water',
'elementType': 'geometry',
'stylers': [{'visibility': 'on'}, {'hue': '#5f94ff'}, {'lightness': 60}]
}];
var map;
var censusMin = Number.MAX_VALUE, censusMax = -Number.MAX_VALUE;
// initMap function initializes the map object and adds map when the web page loads.
function initMap() {
// load the map
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 40, lng: -100},
zoom: 4,
styles: mapStyle
});
// set up the style rules and events for google.maps.Data
map.data.setStyle(styleFeature);
map.data.addListener('mouseover', mouseInToRegion);
map.data.addListener('mouseout', mouseOutOfRegion);
// wire up the button
var selectBox = document.getElementById('census-variable');
google.maps.event.addDomListener(selectBox, 'change', function() {
clearCensusData();
loadCensusData(selectBox.options[selectBox.selectedIndex].value);
});
// state polygons only need to be loaded once, do them now
loadMapShapes();
}
/*
On selecting a data source option from the map control dropdown menu, the map queries
the US Census Data API for the specified variable. To connect the census data with
the shape data, the code sets the "idPropertyName" to 'STATE', which is a common
key in both the Census data and in the GeoJson properties.
*/
// Loads the state boundary polygons from a GeoJSON source
function loadMapShapes() {
// load US state outline polygons from a GeoJSON file
map.data.loadGeoJson('https://storage.googleapis.com/mapsdevsite/json/states.js', { idPropertyName: 'STATE' });
// wait for the request to complete by listening for the first feature to be added
google.maps.event.addListenerOnce(map.data, 'addfeature', function() {
google.maps.event.trigger(document.getElementById('census-variable'), 'change');
});
}
// Loads the census data from a simulated API call to the US Census API.
// @param {string} variable
function loadCensusData(variable) {
// load the requested variable from the census API (using local copies)
var xhr = new XMLHttpRequest();
xhr.open('GET', variable + '.json');
xhr.onload = function() {
var censusData = JSON.parse(xhr.responseText);
censusData.shift(); // the first row contains column names
censusData.forEach(function(row) {
var censusVariable = parseFloat(row[0]);
var stateId = row[1];
// keep track of min and max values
if (censusVariable < censusMin) {
censusMin = censusVariable;
}
if (censusVariable > censusMax) {
censusMax = censusVariable;
}
// update the existing row with the new data
map.data.getFeatureById(stateId).setProperty('census_variable', censusVariable);
});
// update and display the legend
document.getElementById('census-min').textContent = censusMin.toLocaleString();
document.getElementById('census-max').textContent = censusMax.toLocaleString();
};
xhr.send();
}
// Removes census data from each shape on the map and resets the UI
function clearCensusData() {
censusMin = Number.MAX_VALUE;
censusMax = -Number.MAX_VALUE;
map.data.forEach(function(row) {
row.setProperty('census_variable', undefined);
});
document.getElementById('data-box').style.display = 'none';
document.getElementById('data-caret').style.display = 'none';
}
// Applies a gradient style based on 'census_variable' column.
// This the callback passed to data.setStyle() and is called for each row in
// the data set. Check out the docs for Data.StylingFunction.
// @param {google.maps.Data.Feature} feature
function styleFeature(feature) {
var low = [5, 69, 54]; // color of smallest datum
var high = [151, 83, 34]; // color of largest datum
// delta represents where the value sits between the min and max
var delta = (feature.getProperty('census_variable') - censusMin) / (censusMax - censusMin);
var color = [];
for (var i = 0; i < 3; i++) {
// calculate an integer color based on the delta
color[i] = (high[i] - low[i]) * delta + low[i];
}
// determine whether to show this shape or not
var showRow = true;
if (feature.getProperty('census_variable') == null || isNaN(feature.getProperty('census_variable'))) {
showRow = false;
}
var outlineWeight = 0.5, zIndex = 1;
if (feature.getProperty('state') === 'hover') {
outlineWeight = zIndex = 2;
}
return {
strokeWeight: outlineWeight,
strokeColor: '#fff',
zIndex: zIndex,
fillColor: 'hsl(' + color[0] + ', ' + color[1] + '%, ' + color[2] + '%)',
fillOpacity: 0.75,
visible: showRow
};
}
// Responds to the mouse-in event on a map shape (state)
// @param {?google.maps.MouseEvent} e
function mouseInToRegion(e) {
// set the hover state so the setStyle function can change the border
e.feature.setProperty('state', 'hover');
var percent = (e.feature.getProperty('census_variable') - censusMin) / (censusMax - censusMin) * 100;
// update the label
document.getElementById('data-label').textContent = e.feature.getProperty('NAME');
document.getElementById('data-value').textContent = e.feature.getProperty('census_variable').toLocaleString();
document.getElementById('data-box').style.display = 'block';
document.getElementById('data-caret').style.display = 'block';
document.getElementById('data-caret').style.paddingLeft = percent + '%';
}
// Respond to mouse-out event on a map shape (state)
// @param (?google.maps.MouseEvent) e
function mouseOutOfRegion(e) {
// reset the hover state, returning the border to normal
e.feature.setProperty('state', 'normal');
}
</script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCgncHE9VhehCiKENUJB0y19GURax6WXqk&callback=initMap" async defer></script>
</body>
</html>