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.

113 lines
2.7 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 visible_layers = [];
  13. var visible_data = [];
  14. var animatedMarker;
  15. var timers = [];
  16. function resetAnimation() {
  17. if(animatedMarker) {
  18. map.removeLayer(animatedMarker);
  19. }
  20. if(timers.length > 0) {
  21. for(var i in timers) {
  22. clearTimeout(timers[i]);
  23. }
  24. }
  25. }
  26. jQuery(function($){
  27. $('.calendar a').click(function(evt){
  28. var append = evt.altKey;
  29. if(!append) {
  30. $('.calendar a').removeClass('selected');
  31. if(visible_layers.length) {
  32. for(var i in visible_layers) {
  33. map.removeLayer(visible_layers[i]);
  34. }
  35. }
  36. visible_layers = [];
  37. visible_data = [];
  38. }
  39. $(this).addClass('selected');
  40. resetAnimation();
  41. var db_name = $("#database").data("name");
  42. var db_token = $("#database").data("token");
  43. $.get("/api/query?format=linestring&date="+$(this).data('date')+"&tz=America/Los_Angeles&token="+db_token, function(data){
  44. if(data.coordinates && data.coordinates.length > 0) {
  45. visible_data.push(data);
  46. visible_layers.push(L.geoJson(data, {
  47. style: geojsonLineOptions
  48. }).addTo(map));
  49. // If the new layer is completely outside the current view, zoom the map to fit all layers
  50. var layer = visible_layers[visible_layers.length - 1];
  51. var is_outside = false;
  52. if(!map.getBounds().intersects(layer.getBounds())) {
  53. is_outside = true;
  54. }
  55. if(is_outside) {
  56. var full_bounds;
  57. for(var i in visible_layers) {
  58. layer = visible_layers[i];
  59. if(full_bounds) {
  60. full_bounds.extend(layer.getBounds());
  61. } else {
  62. full_bounds = layer.getBounds();
  63. }
  64. }
  65. map.fitBounds(full_bounds);
  66. }
  67. }
  68. });
  69. return false;
  70. });
  71. function pointFromGeoJSON(geo) {
  72. return L.latLng(geo[1], geo[0])
  73. }
  74. $('#btn-play').click(function(){
  75. console.log(visible_data[0].coordinates[0]);
  76. var point = pointFromGeoJSON(visible_data[0].coordinates[0]);
  77. resetAnimation();
  78. animatedMarker = L.marker(point);
  79. animatedMarker.addTo(map);
  80. timers = [];
  81. var interval = 3;
  82. for(var i in visible_data[0].coordinates) {
  83. (function(i){
  84. timers.push(setTimeout(function(){
  85. point = pointFromGeoJSON(visible_data[0].coordinates[i]);
  86. animatedMarker.setLatLng(point);
  87. }, interval*i));
  88. })(i);
  89. }
  90. });
  91. });