-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathindex.html
268 lines (240 loc) · 10.5 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>Starter Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="//cdn.datatables.net/plug-ins/1.10.7/integration/bootstrap/3/dataTables.bootstrap.css">
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.5/mapbox.css' rel='stylesheet'>
<link rel="stylesheet" href="daterangepicker.css">
<link rel="stylesheet" href="jquery.nouislider.css">
<link rel="stylesheet" href="style.css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="starter-template">
<h1>Map SFPD Incident Reports</h1>
<form id="formaddress" class="form-inline">
<div class="form-group" id="searchgroup">
<label for="exampleInputEmail1">Address</label>
<input type="text" class="form-control typeahead" id="inputAddress" placeholder="Enter address" autocomplete="off">
<ul id="suggestresults" style="display:none;">
</ul>
</div>
<div class="form-group">
<label for="range-slider-input">Radius</label>
<input id="range-slider-input" class="form-control" type="text" style="width:60px; text-align: right;"> ft
</div>
<div class="form-group">
<div style="width:150px; float:right;">
<div id="range-slider"></div>
</div>
</div>
<div class="form-group">
<label for="select-time">Time period</label>
<input id="daterange" type="text" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Map Incidents</button>
</form>
</div>
<div id="map" style="width:100%; height:500px; margin-top: 20px;">
<div class="loading" style="display:none;">
<div class="circle"></div>
</div>
</div>
<a href="" id="download-csv"><button class="btn btn-sm btn-primary download-csv"><span class="glyphicon glyphicon-arrow-down"></span> Download CSV</button></a>
<a href="" id="open-geojsonio"><button class="btn btn-sm btn-primary open-geojsonio"><span class="glyphicon glyphicon-arrow-down"></span> Open with Geojson.io</button></a>
<a hrer="" id="open-cartodb"><button class="btn btn-sm btn-primary open-cartodb"><span class="glyphicon glyphicon-arrow-down"></span> Open with CartoDB</button></a>
<table id="example" class="display table" cellspacing="0" width="100%">
<thead>
<tr>
<th>IncidntNum</th>
<th>Category</th>
<th>Descript</th>
<th>Resolution</th>
</tr>
</thead>
<tfoot>
<tr>
<th>IncidntNum</th>
<th>Category</th>
<th>Descript</th>
<th>Resolution</th>
</tr>
</tfoot>
</table>
</div>
<!-- /.container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.5/mapbox.js'></script>
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
<script src="//cdn.datatables.net/plug-ins/1.10.7/integration/bootstrap/3/dataTables.bootstrap.js"></script>
<script src='moment.js'></script>
<script src='daterangepicker.js'></script>
<script src="jquery.nouislider.all.min.js"></script>
<script src="typeahead.bundle.js"></script>
<script>
var table;
$(function() {
var startD = moment().subtract(29, 'days').format('YYYY-MM-DD'),
endD = moment().format('YYYY-MM-DD');
$('#daterange').val(moment().subtract(29, 'days').format('MM/DD/YYYY') + ' - ' + moment().format('MM/DD/YYYY'))
$('#daterange').daterangepicker({
ranges: {
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
'This Quarter': [moment().startOf('quarter'), moment().endOf('quarter')],
'Last Quarter': [moment().subtract(3, 'months').startOf('quarter'), moment().subtract(3, 'months').endOf('quarter')],
'This Year': [moment().startOf('year'), moment()],
'Last Year': [moment().subtract(1, 'year').startOf('year'), moment().subtract(1, 'year').endOf('year')]
},
startDate: moment().subtract(29, 'days'),
endDate: moment(),
format: 'MM/DD/YYYY'
},
function(start, end) {
startD = start.format('YYYY-MM-DD');
endD = end.format('YYYY-MM-DD');
$('#daterange').val(start.format('MM/DD/YYYY') + ' - ' + end.format('MM/DD/YYYY'));
}
);
$('#range-slider').noUiSlider({
start: [1320],
step: 1,
connect: 'lower',
range: {
'min': [0],
'max': [5280]
}
});
$('#range-slider').on({
slide: function() {
radius.setRadius($(this).val() * .3048);
}
})
$("#range-slider").Link('lower').to($('#range-slider-input'), null, wNumb({
decimals: 0
}));
L.mapbox.accessToken = 'pk.eyJ1IjoiZGF0YXNmIiwiYSI6Ilo3bVlHRDQifQ.7gkiPnZtioL8CnCvJ5z9Bg';
var map = L.mapbox.map('map', 'datasf.j9b9ihf0').setView([37.767806, -122.438153], 12);
var geoJsonLayer = L.geoJson();
var center = L.mapbox.featureLayer().addTo(map);
var centerLat, centerLon;
var radius = L.circle([50.5, 30.5], 200).addTo(map);
var geoJsonLayer = L.mapbox.featureLayer().addTo(map);
var feet = $('#range-slider-input').val();
var meters = feet * .3048;
/*
var addresses = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: 'http://api.tiles.mapbox.com/v4/geocode/mapbox.places/%QUERY.json?access_token=pk.eyJ1IjoiZGF0YXNmIiwiYSI6Ilo3bVlHRDQifQ.7gkiPnZtioL8CnCvJ5z9Bg'
});
addresses.initialize();
$('.typeahead').typeahead(null, {
name: 'addresses',
displayKey: 'value', // if not set, will default to 'value',
source: addresses.ttAdapter()
});*/
$('.typeahead').on('keypress', function(e) {
console.log(encodeURIComponent($(e.target).val()));
if ($(e.target).val().length > 3) {
var url = "//search.mapzen.com/v1/autocomplete"
var params = {
api_key: 'search-kz-89WY',
text: $(e.target).val(),
'focus.point.lat': 37.76,
'focus.point.lon': -122.43
}
$.getJSON(url, params, function(d) {
$("#suggestresults").empty();
$("#suggestresults").show();
console.log(d);
$.each(d.features, function(key, val) {
var result = $('<li>').html(val.properties.label);
result.attr('data-index', key);
$("#suggestresults").append(result);
});
$("#suggestresults li").on('click', function(e) {
center.setGeoJSON(d.features[$(e.target).attr('data-index')]);
centerLat = d.features[$(e.target).attr('data-index')].geometry.coordinates[1];
centerLon = d.features[$(e.target).attr('data-index')].geometry.coordinates[0];
$('#inputAddress').val($(e.target).html());
$('#suggestresults').hide();
map.panTo([centerLat, centerLon]);
radius.setLatLng([centerLat, centerLon])
radius.setRadius(meters);
})
});
}
});
table = $('#example').DataTable( {
"ajax": {
"url": "empty.json",
"dataSrc": ""
},
"columns": [
{ "data": "incidntnum" },
{ "data": "category" },
{ "data": "descript" },
{ "data": "resolution" }
],
"pageLength": 50
});
console.log(table);
$('#formaddress').submit(function(e) {
$(".loading").show();
var start = new Date();
feet = $('#range-slider-input').val();
meters = feet * .3048;
var datageojson;
var datasetAPI = "https://data.sfgov.org/resource/cuks-n6tp";
var appToken = "W4RBmRyo2ORX3liaMhmwRjMHT"
var query = "?$where=date >= '" + startD + "' AND date <= '" + endD + "' AND within_circle(location," + centerLat + "," + centerLon + "," + meters + ")&$order=date DESC";
var datasetUrlGeo = datasetAPI + ".geojson" + query;
$.getJSON(datasetUrlGeo, function(data) {
var dataTime = new Date();
var geojsondata = data;
//geojsondata = toGeojson(data, 'location');
radius.setLatLng([centerLat, centerLon])
radius.setRadius(meters);
geoJsonLayer.on('layeradd', function(e) {
var marker = e.layer,
feature = marker.feature;
var popupContent = feature.properties.category;
marker.bindPopup(popupContent);
});
geoJsonLayer.setGeoJSON(geojsondata);
$(".loading").hide();
map.fitBounds(radius.getBounds());
});
var datasetUrlJson = datasetAPI + ".json" + query;
var datasetUrlCsv = datasetAPI + ".csv" + query;
table.ajax.url(datasetUrlJson).load();
//var file = "https://data.sfgov.org/api/views/cuks-n6tp/rows.csv?accessType=DOWNLOAD";
$('#open-cartodb').attr("href","//oneclick.cartodb.com/?file=" + encodeURIComponent(encodeURI(datasetUrlGeo)) + "&provider=DataSF"); //and logo=
$('#download-csv').attr("href",datasetUrlCsv);
$("#open-geojsonio").attr("href","http://geojson.io/#data=data:text/x-url,"+encodeURIComponent(datasetUrlGeo));
e.preventDefault();
});
});
</script>
</body>
</html>