/
GMaps.php
366 lines (319 loc) · 10.3 KB
/
GMaps.php
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
<? require_once("include/functions.inc"); ?>
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
Array.prototype.max = function() {
var max = this[0];
var len = this.length;
for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
return max;
}
Array.prototype.min = function() {
var min = this[0];
var len = this.length;
for (var i = 1; i < len; i++) if (this[i] < min) min = this[i];
return min;
}
var myPolygons;
var allLats;
var allLngs;
var allDates;
var meanLat;
var meanLng;
var centerLat;
var centerLng;
var varLat;
var varLng;
var minLat;
var minLng;
var maxLat;
var maxLng;
var myMap;
var opacity;
var N;
var overlayNum;
var numEvents;
function retrieveRows() {
<?
if (!isset($db)) {
$db = dbConnect('localhost', 'GoogleHeatMap');
mysql_select_db('GoogleHeatMap');
}
$mainQuery = "select * from TucsonCrime;";
$mainResult = mysql_query($mainQuery);
if (!$mainResult) { echo "Error!"; }
else {
if (isset($db)) { mysql_close($db); unset($db); }
if (mysql_num_rows($mainResult) > 0) {
$i = 0;
while ($row = mysql_fetch_assoc($mainResult)) {
$allLats[$i] = $row['Latitude'];
$allLngs[$i] = $row['Longitude'];
$allDates[$i] = $row['EventDate'];
$i++;
}
}
$numEvents = $i;
?>
allLats = new Array(
<?
$i = 0;
while ($i < $numEvents) {
echo "$allLats[$i]";
if ($i < $numEvents-1) {
echo ",";
}
$i++;
}
?>);
allLngs = new Array(
<?
$i = 0;
while ($i < $numEvents) {
echo "$allLngs[$i]";
if ($i < $numEvents-1) {
echo ",";
}
$i++;
}
?>);
allDates = new Array(
<?
$i = 0;
while ($i < $numEvents) {
echo "$allDates[$i]";
if ($i < $numEvents-1) {
echo ",";
}
$i++;
}
?>);
<?
}
?>
numEvents = <? echo $numEvents; ?>;
}
function drawPolygons(N, newOpacity) {
var lowerLat = [];
var lowerLng = [];
var upperLat = [];
var upperLng = [];
var lngSpan;
var latSpan;
var binLngSpan;
var binLatSpan;
var freqArr = new Array(N);
for (i = 0; i < N; i++) {
freqArr[i] = new Array(N);
for (j = 0; j < N; j++) {
freqArr[i][j] = 0;
lowerLat[i] = minLat + i*(maxLat-minLat)/N;
upperLat[i] = minLat + (i+1)*(maxLat-minLat)/N;
lowerLng[i] = minLng + i*(maxLng-minLng)/N;
upperLng[i] = minLng + (i+1)*(maxLng-minLng)/N;
}
}
lngSpan = maxLng - minLng;
latSpan = maxLat - minLat;
binLngSpan = lngSpan / N;
binLatSpan = latSpan / N;
var ybin;
var xbin;
for (i = 0; i < allLats.length; i++) {
ybin = parseInt((allLngs[i] - minLng) / lngSpan * N);
xbin = parseInt((allLats[i] - minLat) / latSpan * N);
if (xbin < 0 || xbin > (N - 1)) continue;
if (ybin < 0 || ybin > (N - 1)) continue;
freqArr[xbin][ybin]++;
}
var maxFreq = 0;
for (i = 0; i < N; i++) {
if (maxFreq < freqArr[i].max()) {
maxFreq = freqArr[i].max();
}
}
overlayNum = 0;
var intensity;
var colorDecimal;
var colorHex;
var colorString;
myPolygons = new Array(N*N);
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
intensity = freqArr[i][j] / maxFreq;
if (intensity <= 0.33) {
colorDecimal = parseInt(255 * intensity / 0.33);
colorHex = parseInt(colorDecimal, 10).toString(16);
if (colorHex.length < 2) colorHex = "0" + colorHex;
colorString = "#";
colorString += colorHex;
colorString += "0000";
} else if (intensity <= 0.66) {
colorDecimal = parseInt(255 * (intensity-0.33) / 0.33);
colorHex = parseInt(colorDecimal, 10).toString(16);
if (colorHex.length < 2) colorHex = "0" + colorHex;
colorString = "#ff";
colorString += colorHex;
colorString += "00";
} else if (intensity > 0.66) {
colorDecimal = parseInt(255 * (intensity-0.66) / 0.33);
colorHex = parseInt(colorDecimal, 10).toString(16);
if (colorHex.length < 2) colorHex = "0" + colorHex;
colorString = "#ffff";
colorString += colorHex;
}
if (colorString == "#000000") {
continue;
} else {
<?
if (isset($_GET['Opacity'])) { ?>
opacity = <? echo $_GET['Opacity']; ?>; <?
} else { ?>
opacity = newOpacity; <?
}
?>
}
var boxCoords = [new google.maps.LatLng(lowerLat[i], lowerLng[j]),
new google.maps.LatLng(lowerLat[i], upperLng[j]),
new google.maps.LatLng(upperLat[i], upperLng[j]),
new google.maps.LatLng(upperLat[i], lowerLng[j])];
myPolygons[overlayNum] = new google.maps.Polygon({
paths: boxCoords,
strokeColor: "#000000",
strokeOpacity: 0.0,
strokeWeight: 0,
fillColor: colorString,
fillOpacity: opacity});
myPolygons[overlayNum].setMap(myMap);
overlayNum++;
}
}
}
function initialize() {
retrieveRows();
document.getElementById("numEvents").innerHTML = numEvents;
var minDate = "<? echo min($allDates); ?>";
var maxDate = "<? echo max($allDates); ?>";
var numRows = <? echo $i; ?>;
document.getElementById("startDate").innerHTML = minDate;
document.getElementById("endDate").innerHTML = maxDate;
meanLat = <? echo array_sum($allLats) / sizeof($allLats); ?>;
meanLng = <? echo array_sum($allLngs) / sizeof($allLngs); ?>;
var initZoom;
var i;
var j;
<?
if (isset($_GET['Lat'])) { ?>
centerLat = <? echo $_GET['Lat']; ?>; <?
} else { ?>
centerLat = meanLat; <?
}
if (isset($_GET['Lng'])) { ?>
centerLng = <? echo $_GET['Lng']; ?>; <?
} else { ?>
centerLng = meanLng; <?
}
if (isset($_GET['Zoom'])) { ?>
initZoom = <? echo $_GET['Zoom']; ?>; <?
} else { ?>
initZoom = 11; <?
}
?>
var myLatLng = new google.maps.LatLng(centerLat, centerLng);
var myOptions = {
zoom: initZoom,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.TERRAIN};
myMap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var varLat = 0;
var varLng = 0;
for (i = 0; i < allLats.length; i++) {
varLat += Math.pow(allLats[i]-meanLat, 2);
varLng += Math.pow(allLngs[i]-meanLng, 2);
}
varLat /= allLats.length;
varLng /= allLngs.length;
minLng = meanLng - 40*varLat;
maxLng = meanLng + 40*varLat;
maxLat = meanLat + 40*varLat;
minLat = meanLat - 40*varLat;
<?
if (isset($_GET['Resolution'])) { ?>
N = <? echo $_GET['Resolution']; ?>; <?
} else { ?>
N = 25; <?
}
?>
drawPolygons(N, 0.8);
}
function killPolygons() {
for (i = 0; i < overlayNum; i++) {
myPolygons[i].setMap(null);
myPolygons[i] = null;
}
}
function changeOpacity(newOpacity) {
for (i = 0; i < overlayNum; i++) {
myPolygons[i].setOptions({fillOpacity: newOpacity});
}
}
function changeResolution(newResolution, newOpacity) {
if (newResolution != N) {
killPolygons();
drawPolygons(newResolution, newOpacity);
}
}
function addAddress() {
var geocoder = new google.maps.Geocoder();
var address = document.getElementById("address").value;
if (geocoder) {
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: myMap,
position: results[0].geometry.location});
}
});
}
}
</script>
</head>
<body onload="initialize()">
<div align=center>
<div style="margin-bottom: 10px;">
Location: <input type="text" name="address"
id="address"
onChange="addAddress()"/>
<input type="button" value="Drop Pin" onClick=addAddress()"/>
</div>
<div id="map_canvas" style="width: 800px; height: 600px"></div>
<div>
<form name="input" action="GMaps.php" method="get">
Resolution: <input type="range" name="Resolution"
min="1" max="100" step="5"
value="25"
onChange="changeResolution(
this.value,getElementsByName('Opacity')[0].value)" />
Opacity: <input type="range" name="Opacity"
min="0" max="1.0" step="0.01"
value="0.5"
onChange="changeOpacity(this.value)" />
</form>
</div>
<div style="width: 800px">This map shows police incident density in Tucson,
showing <div id="numEvents" style="display: inline;"> </div> events over
the period beginning <div id="startDate" style="display: inline;"> </div>
and ending <div id="endDate" style="display: inline;"> </div>.
Data is currently sourced from
<a href="http://maps.azstarnet.com/crime/show30">the AZ Star website</a>,
because the Tucson police department is reluctant to interface with the
unwashed masses of the World Wide Web.</div>
<br><br>
</div>
</body>
</html>