forked from radumas/bikeways4everybody
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
630 lines (558 loc) · 23 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
<!DOCTYPE html>
<!--Based on https://github.com/mjfoster83/web-map-workshop/blob/master/7_advancedMapping_CartoDB/index-completed.html-->
<html>
<head>
<meta charset=utf-8 />
<title>Bikeways for Everybody!</title>
<!-- Mapbox-->
<script src="https://api.mapbox.com/mapbox.js/v2.2.4/mapbox.js"></script>
<link rel="stylesheet" href="https://api.mapbox.com/mapbox.js/v2.2.4/mapbox.css" />
<!-- /Mapbox-->
<!-- Leaflet.draw-->
<link rel="stylesheet" type="text/css" href="css/leaflet.draw.css" />
<script src="https://leaflet.github.io/Leaflet.draw/leaflet.draw.js"></script>
<!-- /Leaflet.draw-->
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<!-- QGIS Webmap-->
<link rel="stylesheet" href="css/label.css" />
<script src="js/label.js"></script>
<!-- /QGIS Webmap-->
<!-- Impromptu (for walkthrough)-->
<link rel="stylesheet" href="jQuery-Impromptu/dist/jquery-impromptu.min.css" />
<script src="jQuery-Impromptu/dist/jquery-impromptu.min.js"></script>
<!-- /Impromptu -->
</head>
<body>
<div id="wrapper">
<div id="map" title=""></div>
<div id="dialogGlobal" title="About You">
<form>
<fieldset style="border: none;">
<ul style="list-style-type: none; padding-left: 0px">
<li><label for="username">Please Enter Your First Name</label></li>
<li><input type="text" name="username" id="username" placeholder="Enter your name" pattern="[A-Za-z]{3,}" title="Your first name must be at least 3 letters" class="text ui-widget-content ui-corner-all" required></li>
<li><label for="zipcode">Please Enter Your Zipcode</label></li>
<li><input type="text" name="zipcode" maxlength="5" pattern="[0-9]{5}" id="zipcode" title="Please enter a 5 digit zip-code" placeholder="02134" class="text ui-widget-content ui-corner-all" required></li>
</ul>
<input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
</fieldset>
</form>
</div>
<div id="dialog" title="Tell Us About This">
<form>
<fieldset style="border: none;">
<ul style="list-style-type: none; padding-left: 0px">
<li><label for="description">About this</label></li>
<li><input type="text" name="description" id="description" placeholder="Description for this" class="text ui-widget-content ui-corner-all"></li>
</ul>
<input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
</fieldset>
</form>
</div>
<script src="js/definitions.js"></script>
<script src="js/directions.js"></script>
<script src="data/json_bikefacilitiesexp.js"></script>
<script src="js/userconfig.js"></script>
<script>
L.mapbox.accessToken =
config.mapboxAccessToken;
// Create a map in the div #map
var bounds = L.latLngBounds(L.latLng(config.south,config.west), L.latLng(config.north,config.east));
var map = L.mapbox.map('map', config.baseMap,{
center: config.mapFocus,
zoom: 13, minZoom : 13, maxzoom: 17, maxBounds : bounds, zoomControl :false });
L.control.zoom({position : 'bottomleft'}).addTo(map);
//Empty to store markers after they are submitted
var submittedData = L.geoJson(false, {
onEachFeature: function (feature, layer) {
layer.bindPopup('<b>'+feature.properties.name +', '+ feature.properties.zipcode +'</b><br>' +feature.properties.description +'');
}
}).addTo(map);
//Bug appeared preventing ending of route drawing.
map.doubleClickZoom.disable();
// Do nothing on right-click
map.on('contextmenu', []);
//Adding the left Bar of icons as a leaflet control panel
var leftBar = L.control({position: 'topleft'});
//Inspiration: http://stackoverflow.com/a/25764322/4047679
leftBar.onAdd = function(map){
var div =L.DomUtil.create('div', '');
div.id = 'leftBar';
div.innerHTML = '<div id="add-route" class="circle type1 icon-hover tooltip" title="Draw routes on the map where you want improved infrastructure">'
+'<i class="fa fa-pencil fa-3x fa-inverse"></i>'
+'</div>'
+' <div id="add-point" class="circle type1 icon-hover tooltip" title="Add points to the map where you want improved infrastructure">'
+'<i class="fa fa-map-marker fa-inverse fa-3x"></i>'
+'</div><br>'
+'<div id="about" class="circle type1 icon-hover tooltip" title="Learn more">'
+'<i class="fa fa-info fa-3x fa-inverse"></i>'
+'</div> <br>'
+'<div id="cancel" class="circle type1 icon-hover tooltip" title="Cancel drawing" style="inline-block">'
+'<i class="fa fa-times fa-3x fa-inverse"></i>'
+'</div>'
+' <div id="save" class="circle type1 icon-hover tooltip" title="Save Route" style="inline-block">'
+'<i class="fa fa-floppy-o fa-3x fa-inverse"></i></div>';
return div;
};
leftBar.addTo(map);
//Adding layer of labels
//See http://gis.stackexchange.com/a/178633/36886
var CartoDB_PositronOnlyLabels = {
"tilejson": "2.0.1",
"tiles":['http://c.basemaps.cartocdn.com/light_only_labels/{z}/{x}/{y}.png'],
"attribution": '© <a href="http://cartodb.com/attributions">CartoDB</a>',
"maxzoom": 19,
"minzoom": 13
};
var topPane = map._createPane('leaflet-top-pane', map.getPanes().mapPane);
var labelLayer = L.mapbox.tileLayer(CartoDB_PositronOnlyLabels).addTo(map);
topPane.appendChild(labelLayer.getContainer());
//See for explanation of zindex level http://stackoverflow.com/q/35092858/4047679
labelLayer.setZIndex(5);
//Empty to store drawn routes after they are submitted
var submittedRoutes = new L.geoJson(false, {
onEachFeature: function (feature, layer) {
layer.bindPopup('<b>'+feature.properties.name +', '+ feature.properties.zipcode +'</b><br>' +feature.properties.description +'');
}
,style: function(feature){
/* Styles the submitted route drawing with the same properties as the drawn route as defined by line(id) in definitions.js */
return{
color:feature.properties.color,
weight: feature.properties.weight,
opacity: feature.properties.opacity
}
}
}).addTo(map);
var drawnMarkers = new L.FeatureGroup();
map.addLayer(drawnMarkers);
var drawnRoute = new L.FeatureGroup();
var drawControl = new L.Control.Draw({
draw : {
polygon : false,
polyline : false,
rectangle : false,
circle : false
},
edit : false,
remove: false
});
markerDrawer = new L.Draw.Marker(map, drawControl.options.marker);
map.on('draw:created', function (e) {
var layer = e.layer;
drawnMarkers.addLayer(layer);
dialog.dialog( "open" );
});
/*#left-bar buttons*/
/* button that triggers adding a marker */
$("#add-point").on("click", function(event){
event.stopPropagation();
if(validInput===false){
dialogGlobal.data('clicked','add-point').dialog( "open" );
}
else if(validInput){
drawingPoints();
}
});
/* button that triggers drawing a route */
$("#add-route").on("click", function(event) {
event.stopPropagation();
if(validInput===false){
dialogGlobal.data('clicked','add-route').dialog( "open" );
}
else if(validInput){
drawingRoute();
}
});
/*Button that triggers the initial walkthrough again*/
$("#about").on("click", function(event){
event.stopPropagation();
$.prompt(tourStates);
});
$("#cancel").on("click", function(event){
event.stopPropagation();
if(routeDraw){
cancelLine();
}
else if (markerDraw){
markerDrawer.disable();
};
refreshLayer();
$("#add-route").removeClass('icon-click');
$("#add-point").removeClass('icon-click');
markerDraw = false; routeDraw = false;
$("#cancel").hide();
$("#save").hide();
});
$("#save").on("click", function(event){
event.stopPropagation();
dialog.dialog("open");
});
//Only show when drawing happens
$("#cancel").hide();
$("#save").hide();
/*End #left-bar buttons*/
//Dialog of global user variables
dialogGlobal = $( "#dialogGlobal" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
position: {
my: "center center",
at: "center center",
of: "#map"
},
buttons: [{
text: "Submit",
click: function() {
var checkedValues = dialogGlobalChecker(username.value, zipcode.value);
if (checkedValues.valid){
zip = "'"+zipcode.value+"'";
enteredUsername = "'"+username.value+"'";
dialogGlobal.dialog("close");
validInput = true;
if($(this).data('clicked')=='add-route'){
drawingRoute();
}
else if($(this).data('clicked')=='add-point'){
drawingPoints();
}
}
else{
validInput = false;
var index;
for(index = 0; index < checkedValues.error.length; ++index){
alert(checkedValues.error[index]);
}
}
},
id: "globalAccept"
}],
close: function() {
var checkedValues = dialogGlobalChecker(username.value, zipcode.value);
if (checkedValues.valid){
zip = "'"+zipcode.value+"'";
enteredUsername = "'"+username.value+"'";
dialogGlobal.dialog("close");
validInput = true;
if($(this).data('clicked')=='add-route'){
drawingRoute();
}
else if($(this).data('clicked')=='add-point'){
drawingPoints();
}
}
else{
validInput = false;
var index;
for(index = 0; index < checkedValues.error.length; ++index){
alert(checkedValues.error[index]);
}
}
},
//Hack to avoid "ENTER" leading to a new page...
open: function(){
$("#dialogGlobal").keydown(function(e) {
if (e.keyCode == $.ui.keyCode.ENTER) {
$("#globalAccept").trigger("click");
}
});
}
});
/* dialog that appears after finishing a drawing */
dialog = $( "#dialog" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
position: {
my: "center center",
at: "center center",
of: "#map"
},
buttons: [{
text: "Save Input",
click : function(){
setData();
},
id: "dialogSaveInput"
},
{text: "Cancel",
click: function() {
if(markerDraw){
refreshLayer();
markerDrawer.enable();
}
dialog.dialog("close");
}
}],
close: function() {
if(markerDraw){
refreshLayer();
markerDrawer.enable();
}
}
, open: function() {
var _title = "Tell us about this";
if (markerDraw){
_title += " location";
}
else if (routeDraw){
_title += " route";
}
$( "#dialog" ).dialog( "option", "title",_title);
$( "#dialog" ).keydown(function(e) {
if (e.keyCode == $.ui.keyCode.ENTER) {
$("#dialogSaveInput").trigger("click");
}
});
}
});
function dialogGlobalChecker(name, zip) {
var error = [];
var valid = true;
if(name.length < 3) {
error.push("Your name is too short.");
valid = false;
}
if(zip.length < 5){
error.push("Your zipcode is too short.");
valid = false;
}else if(!/(^\d{5}$)/.test(zip)){
error.push("Your zipcode is invalid.");
valid = false;
}
return {valid: valid, error: error};
}
//Functions to initiate drawing
function drawingRoute(){
$("#add-route").addClass('icon-click');
$("#add-point").removeClass('icon-click');
$("#cancel").show();
markerDrawer.disable();
refreshLayer();
routeDraw = true;
markerDraw = false;
currentLine = startNewLine(routeNum);
map.addLayer(drawnRoute);
drawnRoute.addLayer(currentLine.polyline);
}
function drawingPoints(){
if(routeDraw){
cancelLine();
refreshLayer();
routeDraw = false;
$("#add-route").removeClass('icon-click');
$("#save").hide();
};
$("#add-point").addClass('icon-click');
$("#cancel").show();
markerDrawer.enable();
markerDraw = true;
}
function stopDrawingPoints(){
$("#add-point").removeClass('icon-click');
$("#cancel").hide();
markerDrawer.disable();
markerDraw = false;
}
function setData() {
var enteredDescription = "'"+description.value+"'";
//Convert the drawing to a GeoJSON to pass to the CartoDB sql database
var drawing = "";
if(routeDraw){
var submittedLine = currentLine.polyline.toGeoJSON();
drawing = "'"+JSON.stringify(submittedLine.geometry)+"'";
//To ensure that drawn routes remain on map after saving, with popup.
submittedLine.properties.description = description.value;
submittedLine.properties.name = username.value;
submittedLine.properties.zipcode = zip;
submittedLine.properties.color = currentLine.polyline.options.color;
submittedLine.properties.weight = currentLine.polyline.options.weight;
submittedLine.properties.opacity= currentLine.polyline.options.opacity;
submittedRoutes.addData(submittedLine);
routeNum ++;
stopRouteDraw();
$("#cancel").hide();
$("#save").hide();
};
if(markerDraw){
drawnMarkers.eachLayer(function (layer) {
//Convert the drawing to a GeoJSON to pass to the CartoDB sql database
var newData = layer.toGeoJSON();
drawing = "'"+JSON.stringify(newData.geometry)+"'";
// Transfer drawing to the CartoDB layer
newData.properties.description = description.value;
newData.properties.name = username.value;
newData.properties.zipcode = zip;
submittedData.addData(newData);
});
stopDrawingPoints();
};
refreshLayer();
//Ensures that drawn routes stay on the map.
if(routeDraw){submittedRoutes.eachLayer( function(layer){layer.addTo(map);});};
//Construct the SQL query to insert data from the three parameters: the drawing, the input username, and the input description of the drawn shape
var sql = "SELECT "+ config.cartoDBinsertfunction +"(";
sql += drawing;
sql += ","+enteredDescription;
sql += ","+enteredUsername;
sql += ","+zip+");";
// console.log(sql); //For testing
//Sending the data
$.ajax({
type: 'POST',
url: 'https://'+config.cartoDBusername+'.cartodb.com/api/v2/sql',
crossDomain: true,
data: {"q":sql},
dataType: 'json',
success: function(responseData, textStatus, jqXHR) {
console.log("Data saved");
},
error: function (responseData, textStatus, errorThrown) {
console.log("Problem saving the data");
}
});
dialog.dialog("close");
};
/* QGIS layer*/
var layerOrder = new Array();
var feature_group = new L.featureGroup([]);
function stackLayers() {
for (index = 0; index < layerOrder.length; index++) {
map.removeLayer(layerOrder[index]);
map.addLayer(layerOrder[index]);
}
}
function restackLayers() {
for (index = 0; index < layerOrder.length; index++) {
layerOrder[index].bringToFront();
}
}
map.on('overlayadd', restackLayers);
layerControl = L.control.layers({},{},{collapsed:false});
function doStylebikefacilities(feature) {
if (feature.properties.BikeFacili >= 1.0 &&
feature.properties.BikeFacili <= 1.8) {
return {
color: '#006837',
weight: '3.44',
opacity: '1.0',
}
}
if (feature.properties.BikeFacili >= 1.8 &&
feature.properties.BikeFacili <= 2.6) {
return {
color: '#984ea3',
weight: '3.44',
opacity: '1.0',
}
}
if (feature.properties.BikeFacili >= 2.6 &&
feature.properties.BikeFacili <= 3.4) {
return {
color: '#78c679',
weight: '3.44',
opacity: '1.0',
}
}
if (feature.properties.BikeFacili >= 3.4 &&
feature.properties.BikeFacili <= 4.2) {
return {
color: '#c2e699',
weight: '3.44',
opacity: '1.0',
}
}
if (feature.properties.BikeFacili >= 4.2 &&
feature.properties.BikeFacili <= 5.0) {
return {
color: '#ffff99',
weight: '3.44',
opacity: '1.0',
}
}
}
var json_bikefacilitiesJSON = new L.geoJson(json_bikefacilitiesexp, {
style: doStylebikefacilities
,clickable :false
,pane: 'shadowPane'
});
layerOrder[layerOrder.length] = json_bikefacilitiesJSON;
feature_group.addLayer(json_bikefacilitiesJSON);
L.control.layers({},{'<b>Bike Facilities</b><br /> <img src="img/legend/bikefacilities_Offstreet.png" /> Offstreet<br /> <img src="img/legend/bikefacilities_Cycletrack.png" /> Cycletrack<br /> <img src="img/legend/bikefacilities_Buffered.png" /> Buffered<br /> <img src="img/legend/bikefacilities_Bikelane.png" /> Bikelane<br /> <img src="img/legend/bikefacilities_Sharrow.png" /> Sharrow<br />': json_bikefacilitiesJSON},{collapsed:false}).addTo(map);
L.control.scale({options: {position: 'bottomleft', maxWidth: 100, metric: true, imperial: false, updateWhenIdle: false}}).addTo(map);
stackLayers();
function refreshLayer() {
if(markerDraw){
map.removeLayer(drawnMarkers);
drawnMarkers = new L.FeatureGroup();
}else if(routeDraw){
map.removeLayer(drawnRoute);
drawnRoute = new L.FeatureGroup();
}
};
/* Impromptu dialogs */
var tourSubmitFunc = function(e,v,m,f){
if(v === -1){
$.prompt.prevState();
return false;
}
else if(v === 1){
$.prompt.nextState();
return false;
}
else if(v === 0){
$.prompt.close();
return false;
}
},
tourStates = [
{
title: "Welcome",
html: config.walkthroughWelcome,
buttons:{'I already know':0,'How do I use this?':1},
focus: 1,
submit: tourSubmitFunc
},
{
title:"Data Layers",
html:"Here you have the legend for the datalayers, you can toggle each layer by clicking on the check mark",
buttons:{'Go back':-1,'I\'m done':0,'Tell me more!':1},
focus:2,
position: { container: '.leaflet-control-layers-expanded', x: -330, y: 0, width: 300, arrow: 'rt' },
submit:tourSubmitFunc
},
{
title:"Enable Drawing",
html:"Click on either <span class='fa-stack fa-lg'><i class='fa fa-circle fa-stack-2x'></i><i class='fa fa-map-marker fa-inverse fa-stack-1x'></i></span> or <span class='fa-stack fa-lg'><i class='fa fa-circle fa-stack-2x'></i><i class='fa fa-pencil fa-inverse fa-stack-1x'></i></span> to start giving your input. First we'll want you to enter your first name and zipcode.<br> After that you'll be able to add your input to the map!",
buttons:{'Go back':-1,'I\'m done':0,'Tell me more!':1},
position: { container: '#leftBar', x: 150, y: 0, width: 300, arrow: 'lt' },
focus:2,
submit:tourSubmitFunc
},
{
title:"Click on <span class='fa-stack fa-lg'><i class='fa fa-circle fa-stack-2x'></i><i class='fa fa-map-marker fa-inverse fa-stack-1x'></i></span> and Place a Marker on the Map",
html:"<div class = 'img-container'><img src='img/add_marker.gif'></div><br>Tell us about dangerous intersections, potholes, etc. When you click 'Save Input' your marker and description are sent to our database.",
buttons:{'Go back':-1,'I\'m done':0,'Tell me more!':1},
position:{width:500},
focus:2,
submit:tourSubmitFunc
},
{
title:"Or Click on <span class='fa-stack fa-lg'><i class='fa fa-circle fa-stack-2x'></i><i class='fa fa-pencil fa-inverse fa-stack-1x'></i></span> to Draw a Route",
html:"<div class = 'img-container'><img src='img/draw_route.gif'></div><br>Routes can indicate your entire commute, or small locations where bike lanes are missing, or frequently blocked... Click to add points to form a route. Double click on the final point to end that route, and then write a description. You are welcome to input as many routes as you please",
buttons:{'Go back':-1,'I\'m ready!':0},
position:{width:500},
focus:1,
submit:tourSubmitFunc
}
];
$.prompt(tourStates, {zIndex: 1200});
</script>
</body>
</html>