-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup.js
68 lines (60 loc) · 2.8 KB
/
lookup.js
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
function initMap() {
var map = new google.maps.Map(document.getElementById('maplookup'), {
zoom: 17,
center: {lat: 42.341876, lng: -71.091273},
mapTypeControl : false
});
var geocoder = new google.maps.Geocoder();
document.getElementById('address-submit').addEventListener('click', function() {
geocodeAddress(geocoder, map);
});
}
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address-input').value;
geocoder.geocode({'address': address}, function(results, status) {
if (status === 'OK') {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
var lon = results[0].geometry.location.lng();
var lat = results[0].geometry.location.lat();
var geohash = encodeGeoHash(lat, lon);
var celldata = getCellFromGeohash(geohash);
if(celldata == undefined){
document.getElementById("data2").innerHTML = "No crime data";
document.getElementById("data2").style="color: gray;";
document.getElementById("data1").innerHTML = "No streetlight data";
document.getElementById("data1").style="color: gray;";
}
var lampaverage = 19;
var crimeaverage = 131;
if(celldata[4] < crimeaverage){
document.getElementById("data2").innerHTML = "Crimes reported in area: " + celldata[4];
document.getElementById("data2").style="color: green;";
document.getElementById("info2").innerHTML = "This area is safer than average";
}else{
document.getElementById("data2").innerHTML = "Crimes reported in area: " + celldata[4];
document.getElementById("data2").style="color: red;";
document.getElementById("info2").innerHTML = "This area is more dangerous than average";
}
if(celldata[5] < lampaverage){
document.getElementById("data1").innerHTML = "Streetlights in area: " + celldata[5];
document.getElementById("data1").style="color: red;";
}else{
document.getElementById("data1").innerHTML = "Streetlights in area: " + celldata[5];
document.getElementById("data1").style="color: green;";
}
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
function getCellFromGeohash(gh){
for(var geohash in cells){
if (geohash==gh){
return cells[geohash];
}
}
}