-
Notifications
You must be signed in to change notification settings - Fork 7
/
callers-heatmap.html
154 lines (139 loc) · 3.94 KB
/
callers-heatmap.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
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title>Callers Heatmap</title>
<meta name=viewport content="width=device-width, initial-scale=1.0">
</head>
<body>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
#details {
display: none;
position: absolute;
top: 5em;
left: 1em;
background-color: #EEE;
width: 20em;
border-radius: 0.25em;
border: 1px solid #DDD;
}
.record {
margin: 1em;
}
.name {
font-style: bold;
font-size: 130%;
}
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?libraries=visualization&key=AIzaSyALrS4bNPhHaZ73dACaSxD-r6oht4CX7B8">
</script>
<script>
function fetch(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState==4 && xhr.status==200) {
callback(xhr.responseText);
}
};
xhr.open("GET",url,true);
xhr.send();
}
var zips = null;
var caller_records = null;
window.onload = function() {
fetch("zipcode.json", function(response) {
zips = JSON.parse(response);
maybe_ready();
});
fetch("caller-links.json", function(response) {
caller_records = JSON.parse(response);
maybe_ready();
});
};
var zip_to_caller_records = {}
function add_marker_listener(marker, zip) {
google.maps.event.addListener(marker, 'click', function() {
details.style.display = "block";
details.innerHTML = "";
for (var i = 0; i < zip_to_caller_records[zip].length; i++) {
var caller_record = zip_to_caller_records[zip][i];
var record_div = document.createElement("div");
record_div.className = "record";
var name_span = document.createElement("span");
name_span.className = "name";
name_span.textContent = caller_record.name;
var anchor_destination;
if (caller_record.website && caller_record.website.indexOf(".") != -1) {
if (caller_record.website.indexOf("://") != -1) {
anchor_destination = caller_record.website;
} else {
anchor_destination = "http://" + caller_record.website;
}
}
if (anchor_destination) {
var anchor = document.createElement("a");
anchor.href = anchor_destination;
anchor.appendChild(name_span);
record_div.append(anchor);
} else {
record_div.append(name_span);
}
if (caller_record.email) {
record_div.append(document.createElement("br"));
var email_span = document.createElement("span");
email_span.className = "email";
email_span.textContent = caller_record.email;
record_div.append(caller_record.email);
}
details.appendChild(record_div);
}
});
}
function maybe_ready() {
if (!!zips && !!caller_records) {
var usa = new google.maps.LatLng(39.81,-98.56);
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: usa,
zoom: 5,
});
heatMapData = [];
for (var i = 0 ; i < caller_records.length ; i++) {
var caller_record = caller_records[i];
var zip = caller_record.zip;
if (zip in zips) {
var lat = zips[zip][0];
var lng = zips[zip][1];
var loc = new google.maps.LatLng(lat, lng);
heatMapData.push({
location: loc,
weight: 1
});
if (!(zip in zip_to_caller_records)) {
zip_to_caller_records[zip] = [caller_record];
var marker = new google.maps.Marker({
position: loc,
map: map
});
add_marker_listener(marker, zip);
} else {
zip_to_caller_records[zip].push(caller_record);
}
} else {
console.log("unrecognized zipcode: " + zip);
}
}
var heatmap = new google.maps.visualization.HeatmapLayer({
data: heatMapData
});
heatmap.set('radius', 40);
heatmap.setMap(map);
}
}
</script>
<div id="map-canvas"></div>
<div id="details"></div>
</body> </html>