You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
5.8 KiB

  1. var map = L.map('map', { zoomControl: false }).setView([45.516, -122.660], 14, null, null, 24);
  2. var layer = L.esri.basemapLayer("Topographic");
  3. layer.maxZoom = 24;
  4. layer.maxNativeZoom = 24;
  5. layer.addTo(map);
  6. new L.Control.Zoom({ position: 'topleft' }).addTo(map);
  7. var geojsonLineOptions = {
  8. color: "#0033ff",
  9. weight: 4,
  10. opacity: 0.5
  11. };
  12. var startIcon = L.icon({
  13. iconUrl: '/assets/map-pin-start.png',
  14. iconSize: [18,28],
  15. iconAnchor: [9,28]
  16. });
  17. var endIcon = L.icon({
  18. iconUrl: '/assets/map-pin-end.png',
  19. iconSize: [18,28],
  20. iconAnchor: [9,28]
  21. });
  22. var visible_layers = [];
  23. var visible_data = [];
  24. var highlightedMarker;
  25. var animatedMarker;
  26. var startMarker;
  27. var endMarker;
  28. var timers = [];
  29. function resetAnimation() {
  30. if(animatedMarker) {
  31. map.removeLayer(animatedMarker);
  32. }
  33. if(timers.length > 0) {
  34. for(var i in timers) {
  35. clearTimeout(timers[i]);
  36. }
  37. }
  38. }
  39. function displayLineOnMap(response, options) {
  40. var data = response.linestring;
  41. if(data.coordinates && data.coordinates.length > 0) {
  42. // For any null coordinates, fill it in with the previous location
  43. var lastCoord = null;
  44. for(var i in data.coordinates) {
  45. if(data.coordinates[i] == null) {
  46. data.coordinates[i] = lastCoord;
  47. } else {
  48. lastCoord = data.coordinates[i];
  49. }
  50. }
  51. visible_data.push(data);
  52. visible_layers.push(L.geoJson(data, {
  53. style: geojsonLineOptions
  54. }).addTo(map));
  55. // Show the start/end pins if necessary
  56. if(options.pins) {
  57. startMarker = L.marker(pointFromGeoJSON(visible_data[0].coordinates[0]), {icon: startIcon});
  58. startMarker.addTo(map);
  59. endMarker = L.marker(pointFromGeoJSON(visible_data[0].coordinates[ visible_data[0].coordinates.length-1 ]), {icon: endIcon});
  60. endMarker.addTo(map);
  61. }
  62. // If the new layer is completely outside the current view, zoom the map to fit all layers
  63. var vlayer = visible_layers[visible_layers.length - 1];
  64. var is_outside = false;
  65. if(!map.getBounds().intersects(vlayer.getBounds())) {
  66. is_outside = true;
  67. }
  68. if(is_outside) {
  69. console.log('is outside');
  70. console.log(vlayer);
  71. var full_bounds;
  72. for(var i in visible_layers) {
  73. if(visible_layers[i].getBounds) {
  74. if(full_bounds) {
  75. full_bounds.extend(visible_layers[i].getBounds());
  76. } else {
  77. full_bounds = visible_layers[i].getBounds();
  78. }
  79. }
  80. }
  81. map.fitBounds(full_bounds);
  82. }
  83. showBatteryGraph(response);
  84. }
  85. }
  86. jQuery(function($){
  87. $('.calendar a').click(function(evt){
  88. var append = evt.altKey;
  89. if(!append) {
  90. removeVisibleLayers();
  91. }
  92. $(this).addClass('selected');
  93. resetAnimation();
  94. var db_name = $("#database").data("name");
  95. var db_token = $("#database").data("token");
  96. $.get("/api/query?format=linestring&date="+$(this).data('date')+"&tz="+$("#timezone").val()+"&token="+db_token, function(data){
  97. displayLineOnMap(data, {pins: false});
  98. });
  99. $("#range-from").val($(this).data('date')+' 00:00:00');
  100. $("#range-to").val($(this).data('date')+' 23:59:59');
  101. return false;
  102. });
  103. $('#range-go').click(function(evt) {
  104. var stateObj = {from: $('#range-from').val(), to: $('#range-to').val(), tz: $('#timezone').val()};
  105. var baseURL = "/map/" + $("#database").data('name');
  106. var historyURL = baseURL + "?from="+stateObj.from+"&to="+stateObj.to+"&tz="+stateObj.tz;
  107. window.history.pushState(stateObj, "GPS Log", historyURL)
  108. resetAnimation();
  109. removeVisibleLayers();
  110. $("#range-go").addClass("loading");
  111. var db_token = $("#database").data("token");
  112. $.get("/api/query?format=linestring&start="+$('#range-from').val()+"&end="+$('#range-to').val()+"&tz="+$("#timezone").val()+"&token="+db_token, function(data){
  113. $("#range-go").removeClass("loading");
  114. $("#trip-create-form").removeClass("hidden");
  115. displayLineOnMap(data, {pins: true});
  116. });
  117. return false;
  118. })
  119. $('#btn-play').click(function(){
  120. console.log(visible_data[0].coordinates[0]);
  121. var point = pointFromGeoJSON(visible_data[0].coordinates[0]);
  122. resetAnimation();
  123. animatedMarker = L.marker(point);
  124. animatedMarker.addTo(map);
  125. timers = [];
  126. var interval = 3;
  127. for(var i in visible_data[0].coordinates) {
  128. (function(i){
  129. timers.push(setTimeout(function(){
  130. point = pointFromGeoJSON(visible_data[0].coordinates[i]);
  131. animatedMarker.setLatLng(point);
  132. }, interval*i));
  133. })(i);
  134. }
  135. });
  136. $("#trip-create").click(function(){
  137. $("#trip-create").addClass("loading");
  138. $.post('/api/trip-complete', {
  139. start: $("#range-from").val(),
  140. end: $("#range-to").val(),
  141. tz: $("#timezone").val(),
  142. mode: $("#trip-mode").val(),
  143. token: $("#database").data("write-token")
  144. }, function(response) {
  145. $("#trip-create").removeClass("loading");
  146. });
  147. });
  148. if($("#range-from").val() == "") {
  149. console.log("Autoselecting calendar day");
  150. $(".calendar a[data-date="+((new Date()).toISOString().slice(0,10))+"]").focus().click();
  151. } else {
  152. console.log("Loading range");
  153. $("#range-go").click();
  154. }
  155. });
  156. function removeVisibleLayers() {
  157. $('.calendar a').removeClass('selected');
  158. if(visible_layers.length) {
  159. for(var i in visible_layers) {
  160. map.removeLayer(visible_layers[i]);
  161. }
  162. }
  163. visible_layers = [];
  164. visible_data = [];
  165. if(startMarker) {
  166. map.removeLayer(startMarker);
  167. map.removeLayer(endMarker);
  168. }
  169. }
  170. function moveMarkerToPosition(point) {
  171. if(point.location) {
  172. var coord = pointFromGeoJSON(point.location);
  173. if(coord) {
  174. if(!highlightedMarker) {
  175. highlightedMarker = L.marker(coord).addTo(map);
  176. } else {
  177. highlightedMarker.setLatLng(coord);
  178. }
  179. }
  180. }
  181. }
  182. function pointFromGeoJSON(geo) {
  183. return L.latLng(geo[1], geo[0])
  184. }