-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
272 lines (227 loc) · 8.61 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
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
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<style>
body {
background-color: #EEE;
margin: 0;
padding: 0;
font-family: Tahoma, "Lucida Grande", Verdana, Arial;
font-size: 14px;
color: #555;
}
h2{
margin-top: 30px;
}
h3{
margin-top: -10px;
margin-left: 20px;
border-left: 3px solid #888;
padding-left: 20px;
}
#interface-wrapper{
color: #009900;
font-family: Courier;
font-size: 24px;
font-weight: bold;
}
#canvas-wrapper {
display: inline-block;
background-color: #222222;
box-shadow: 10px 10px 5px #888;
}
#wrapper {
position: relative;
margin: 0 auto;
width: 800px;
margin-top: 20px;
}
#zip-error-underlay {
position: absolute;
top: 20px;
right: 20px;
z-index: 100;
padding: 5px;
width: 70px;
color: #990000;
}
#zip-input-container {
position: absolute;
top: 20px;
right: 20px;
z-index: 200;
}
#zip-input-container input {
font-size: 24px;
font-weight: bold;
width: 81px;
margin: 0px;
border: none;
outline: none;
padding: 5px;
background: none;
font-family: Courier;
color: #009900;
}
#zip-input-container:before {
content: ">";
font-weight: bold;
}
#zip-city-output {
position: absolute;
left: 20px;
top: 360px;
}
#loading {
position: absolute;
left: 280px;
top: 180px;
}
</style>
</head>
<body>
<a href="https://github.com/pastudan/zipcode-explorer"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>
<div id="wrapper">
<div id="interface-wrapper">
<div id=canvas-wrapper>
<canvas id="canvas_element" width="800" height="400"></canvas>
</div>
<div id="zip-error-underlay"></div>
<div id="zip-input-container"><input id="zip-input" maxlength="5" /></div>
<div id="zip-city-output"></div>
<div id="loading">Loading zip codes...</div>
</div>
<div>
<h2>Zip Code Explorer</h2>
<h3>By <a title="Dan Pastusek" href="http://stuffbydan.com">Dan</a></h3>
<p>
Use this tool to visualize US zip codes. Start typing a zip code and and watch it zoom to your target.
</p>
</div>
<div id="sources-container">
<h2>Credit</h2>
<ul>
<li>Original Concept: <a href="http://benfry.com/zipdecode/">Ben Fry</a></li>
<li>Zip Code Database: <a href="http://www.boutell.com/zipcodes/">Tom Boutell</a></li>
<li>Source Code: <a href="https://github.com/pastudan/zipcode-explorer">GitHub</a></li>
<li>License: <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Share Alike</a></li>
</ul>
</div>
</div>
</body>
<script type="text/javascript">
//constants
var canvas_width = 800;
var canvas_height = 400;
var canvas_padding = .15; //padding in percentage
//determine region for drawing selected ZIPs
var active_width = canvas_width - (2 * canvas_padding * canvas_width);
var active_height = canvas_height - (2 * canvas_padding * canvas_height);
//globals
var zipdb;
var tmp_lats_max,
tmp_lats_min,
tmp_longs_max,
tmp_longs_min;
//fetch the zip code database
$.getJSON('zipcode-database.json', function (data) {
zipdb = data;
zip_results = zipdb;
$("#zip-input-container").fadeIn();
$("#loading").fadeOut(function () {
prepare_results();
});
});
//always keep focus on zip input field
$("#zip-input").focus();
$("body").click(function () {
$("#zip-input").focus();
});
//
var canvas = document.getElementById("canvas_element");
var context = canvas.getContext("2d");
context.fillStyle = "#009900";
var zip_results = [];
var number_workers = 10;
//how many times do you want the workers to execute (these aren't true HTML5 workers)
//number_workers is fun to play with .. the more workers, the more fluid the animation is, but also longer.
var worker_interval = 0;
var busy = false;
var lat_delta, long_delta;
$("#zip-input").keyup(function () {
$("#zip-error-underlay").text("");
$this = $(this);
var zip_input = $this.val();
prepare_results(zip_input);
});
function prepare_results(zip_input){
zip_input = zip_input || "";
context.clearRect(0, 0, canvas_width, canvas_height); // clear whole canvas
var ziphtml = "";
var result_count = 0;
var tmp_zip_results = [];
var city_state = "";
for (i in zipdb) {
if (zipdb[i].zip.substring(0, zip_input.length) == zip_input) {
ziphtml += zipdb[i].latitude + ', ' + zipdb[i].longitude;
tmp_zip_results[result_count] = zipdb[i];
result_count++;
}
if (zipdb[i].zip == zip_input) {
city_state = zipdb[i].city + ", " + zipdb[i].state;
}
}
var tmp_lats = [];
var tmp_longs = [];
for (i in tmp_zip_results){
tmp_lats[i] = parseFloat(tmp_zip_results[i].latitude);
tmp_longs[i] = parseFloat(tmp_zip_results[i].longitude);
}
tmp_lats_max = Math.max.apply(null, tmp_lats);
tmp_lats_min = Math.min.apply(null, tmp_lats);
tmp_longs_max = Math.max.apply(null, tmp_longs);
tmp_longs_min = Math.min.apply(null, tmp_longs);
lat_span = tmp_lats_max - tmp_lats_min;
long_span = tmp_longs_max - tmp_longs_min;
console.log("Lat", tmp_lats_max, tmp_lats_min, "Span:", lat_span);
console.log("Long", tmp_longs_max, tmp_longs_min, "Span:", long_span);
$("#zip-city-output").text(city_state);
if (tmp_zip_results.length > 0) {
zip_results = tmp_zip_results; //so we don't accidentally render empty result sets (though this doesn't always catch it)
parse_results();
} else {
$("#zip-input").val(zip_input.slice(0, -1))
$("#zip-error-underlay").stop(true, true).text(zip_input).show().fadeOut(1500);
}
}
function parse_results() {
if (!busy) {
busy = true;
//do work
for (var i = worker_interval; i < zip_results.length; i += number_workers) {
if (2* lat_span > long_span){
x_scale = (active_width / 2 / lat_span);
y_scale = (active_height / lat_span);
} else {
x_scale = (active_width / 2 / long_span);
y_scale = (active_height / long_span);
}
zip_results[i].x = x_scale * (zip_results[i].longitude - tmp_longs_min) + (canvas_padding * canvas_width);
zip_results[i].y = y_scale * (tmp_lats_max - zip_results[i].latitude) + (canvas_padding * canvas_height);
context.fillRect(zip_results[i].x, zip_results[i].y, 2, 2);
}
//end work
busy = false;
// if we aren't done with the workers, wait 1ms and see if the function needs to run again
if (worker_interval < number_workers) {
worker_interval++;
setTimeout(function () {
parse_results();
}, 1);
} else {
worker_interval = 0;
}
}
}
</script>
</html>