-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
392 lines (329 loc) · 12.7 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
<!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, maximum-scale=1, user-scalable=no">
<title></title>
<link rel="stylesheet" href="css/leaflet.css" />
<link rel="stylesheet" href="css/app.css" />
<style type="text/css">
.popup-container {
max-height:200px;
max-width:200px;
overflow: scroll;
}
div.label {
font-weight: bold;
margin-top:10px;
}
form button
{
font-size:large;
}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/parse/1.3.0/parse.js"></script>
<script src="js/leaflet.js"></script>
</head>
<body>
<div id="map-canvas"></div>
<script type="text/javascript">
var map = null;
var current = null;
var current_filter = null;
var current_location_marker = null;
var current_location_circle = null;
var paths = {};
$(function() {
Parse.initialize("SP4alRjyHRf8l5J8XBz7EeNUHYq63YHtw2KSGd1A", "rzTDXw4GTyvp5qKOJb3VrtbaNmcrsUzFvGm8V39R");
map = L.map("map-canvas"); //.setView(L.latLng(61.15368, -150.05472), 10);
L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {
maxZoom:18,
minZoom:12,
id: 'examples.map-20v6611k'
}).addTo(map);
map.on('locationfound', onLocationFound);
map.on('locationerror', onLocationError);
map.locate({setView: true, maxZoom: 17});
//if (navigator.geolocation)
// navigator.geolocation.watchPosition(locationChanged);
$.get("data/paths.geojson", null, function(obj) {
if(obj != null)
{
for (var i = 0; i < obj.features.length; i++)
{
var feature = obj.features[i];
var pathText = feature.properties.path;
var extent = feature.properties.extent;
var extent_parts = extent.split(" ");
var newextent = {};
newextent.min_lon = parseFloat(extent_parts[0]);
newextent.min_lat = parseFloat(extent_parts[1]);
newextent.max_lon = parseFloat(extent_parts[2]);
newextent.max_lat = parseFloat(extent_parts[3]);
if(paths[pathText])
{
paths[pathText].extents.push(newextent);
}
else
{
var newpath = {path:pathText,extents:[]};
newpath.extents.push (newextent);
newpath.isLoaded = false;
paths[pathText] = newpath;
}
}
map.on("viewreset", function(e) {
if(!hasStartedLoadingQueue)
{
loadNextInQueue();
hasStartedLoadingQueue = true;
}
});
}
}, "json");
});
function pathIsInCurrentExtent(path)
{
for (var i = 0; i < path.extents.length; i++) {
if(isInCurrentExtent(path.extents[i]))
return true;
}
return false;
}
function isInCurrentExtent(extent)
{
var bounds = map.getBounds();
var min_lon = bounds.getWest();
var max_lon = bounds.getEast();
var max_lat = bounds.getNorth();
var min_lat = bounds.getSouth();
if(extent.min_lon < max_lon && extent.max_lon > min_lon && extent.min_lat < max_lat && extent.max_lat > min_lat)
return true;
else
return false;
}
var hasStartedLoadingQueue = false;
function loadNextInQueue()
{
var pathNames = keys(paths);
for (var i = 0; i < pathNames.length; i++)
{
var pname = pathNames[i];
var path = paths[pname];
if(path.isLoaded == false && pathIsInCurrentExtent(path))
{
var url = "output/" + path.path;
var xhr = $.get(url, null, function(obj) {
if(obj != null)
{
drawGeoJson(obj);
}
//do it again in a little while
window.setTimeout(loadNextInQueue, 500);
}, "json").fail(function() {
window.setTimeout(loadNextInQueue, 500);
});
path.isLoaded = true;
return;
}
}
//if we got here, nothing need to be loaded within the bounding box
//try again in a second
window.setTimeout(loadNextInQueue, 1000);
}
function locationChanged(position)
{
showCurrentLocation(position.coords.latitude, position.coords.longitude, position.coords.accuracy);
}
function showCurrentLocation(lat, lon, accuracy)
{
var radius = accuracy / 2;
var loc = L.latLng(lat,lon);
if(current_location_marker == null)
{
var locationIcon = L.icon({
iconUrl: 'images/marker-icon.png',
iconRetinaUrl: 'images/marker-icon-2x.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [12, 0],
shadowUrl: 'images/marker-shadow.png',
shadowSize: [41, 41],
shadowAnchor: [10, 41]
});
current_location_marker = L.marker(loc, {icon:locationIcon}).addTo(map);
}
else
current_location_marker.setLatLng(loc);
if(current_location_circle == null)
{
current_location_circle = L.circle(loc, radius).addTo(map);
}
else
{
current_location_circle.setLatLng(loc);
current_location_circle.setRadius(radius);
}
}
function onLocationFound(e)
{
showCurrentLocation(e.latlng.lat, e.latlng.lng, e.accuracy);
}
function onLocationError(e)
{
alert(e.message);
}
function clearAndLoad(url)
{
clearMap();
loadGeoJson(url);
}
function loadGeoJson(url)
{
$.get(url, null, function(obj) {
drawGeoJson(obj);
}, "json");
}
function drawGeoJson(obj)
{
if(obj != null)
{
for (var i = 0; i < obj.features.length; i++)
{
var feature = obj.features[i];
var geom = feature.geometry;
if(geom != null)
{
var points = [];
//we could be working with LineStrings or MultiLineStrings here
if(geom.type == "LineString")
{
for (var j = 0; j < geom.coordinates.length; j++)
{
var ll = L.latLng(geom.coordinates[j][1],geom.coordinates[j][0]);
points.push(ll);
}
}
else if(geom.type == "MultiLineString")
{
for (var j = 0; j < geom.coordinates.length; j++)
{
for(var k = 0; k < geom.coordinates[j].length; k++)
{
var ll = L.latLng(geom.coordinates[j][k][1],geom.coordinates[j][k][0]);
points.push(ll);
}
}
}
var opts = {opacity:0.75, weight:10, color:'green'};
var polyline = L.polyline(points, opts).addTo(map);
polyline.bindPopup(popupHtml(feature));
//feature.polyline = polyline;
}
}
}
}
function keys(obj)
{
var k = [];
for(var key in obj)
{
if(obj.hasOwnProperty(key))
{
k.push(key);
}
}
return k;
}
function clearMap()
{
for(i in map._layers) {
if(map._layers[i]._path != undefined) {
map.removeLayer(map._layers[i]);
}
}
}
function saveTrail(button)
{
var container = $(button).parents("form.trail-form");
var values = container.serializeArray();
var saveme = {};
for (var i = 0; i < values.length; i++) {
saveme[values[i].name] = values[i].value;
}
var checkboxes = container.find(":checkbox");
saveme["summer_usage"] = 0;
saveme["winter_usage"] = 0;
checkboxes.each( function() {
var cb = $(this);
var name = cb.attr("name");
var ischecked = cb.is(":checked");
var value = parseInt(cb.attr("value"));
if(ischecked)
{
saveme[name] += value;
}
});
var TrailObject = Parse.Object.extend("TrailUpdate");
var newTrail = new TrailObject();
newTrail.save(saveme).then(function(object) {
alert("Saved it.");
});
}
function popupHtml(feature)
{
var key = feature.properties["source"] + " / " + feature.properties["system"] + " / " + feature.properties["name"];
var html = "<div class='popup-container'><form class='trail-form'><div><b>" + feature.properties["source"] + " / " + feature.properties["system"] + " / " + feature.properties["name"] + "</b></div>";
html += "<input type='hidden' name='source' value='" + feature.properties["source"] + "'/>";
html += "<input type='hidden' name='system' value='" + feature.properties["system"] + "'/>";
html += "<input type='hidden' name='name' value='" + feature.properties["name"] + "'/>";
html += "<div class='label'>Lighting</div><div>" + dropDown("lighting", ["Yes","No"], feature.properties["lighting"]) + "</div>";
html += "<div class='label'>Surface</div><div>" + dropDown("surface", ["Earth","Asphalt","Gravel","Concrete","Other","Unknown"], feature.properties["surface"]) + "</div>";
html += "<div class='label'>Ski Mode</div><div>" + dropDown("ski_mode", ["Skate","Classic","Both"], feature.properties["ski_mode"]) + "</div>";
html += "<div class='label'>Ski Difficulty</div><div>" + dropDown("ski_difficulty", ["Novice","Intermediate","Advanced","Unknown"], feature.properties["ski_difficulty"]) + "</div>";
html += "<div class='label'>Direction</div><div>" + dropDown("direction", ["2way","1way","1wayreverse","Unknown"], feature.properties["direction"]) + "</div>";
html += "<div class='label'>Handicap Accessible</div><div>" + dropDown("handicap_accessible", ["Yes","No"], feature.properties["handicap_accessible"]) + "</div>";
html += "<div class='label'>Summer Usage</div><div>" + usageFlags("summer_usage", feature.properties["summer_usage"]) + "</div>";
html += "<div class='label'>Winter Usage</div><div>" + usageFlags("winter_usage", feature.properties["winter_usage"]) + "</div>";
html += "<div class='label'>Comments</div><div><textarea rows='5' style='width:90%;' name='comments'></textarea></div>";
html += "<div class='label'>Image URL</div><div><input style='width:90%;' type='text' name='image_url'/></div>";
html += "<p><button type='button' onclick='saveTrail(this);'>Save</button></p>";
html += "</form></div>";
return html;
}
function dropDown(name, options, value)
{
var html = "<select name='" + name + "'><option></option>";
for (var i = 0; i < options.length; i++) {
html += "<option value='" + options[i] + "' " + (value == options[i] ? "selected" : "") + ">" + options[i] + "</option>";
}
html += "</select>";
return html;
}
function usageFlags(name, value)
{
if(!value)
value = 0;
var flags = [];
flags.push([1, 'Foot', false]);
flags.push([2, 'Bike', false]);
flags.push([4, 'Ski', true]);
flags.push([8, 'Snowshoe', true]);
flags.push([16, 'Skijor', true]);
flags.push([32, 'Mush', true]);
flags.push([64, 'Dog Walk', false]);
flags.push([128, 'Off-leash Dog', false]);
var html = "<div>";
for (var i = 0; i < flags.length; i++)
{
if(name == "winter_usage" || flags[i][2] == false )
html += "<div><input name='" + name + "' type='checkbox' " + ((flags[i][0] & value) > 0 ? "checked" : "") + " value='" + flags[i][0] + "'/> " + flags[i][1] + "</div>";
}
html += "</div>";
return html;
}
</script>
</body>
</html>