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.

112 lines
2.6 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?map="+db_name+"&date="+$(this).data('date')+"&tz=America/Los_Angeles&token="+db_token, function(data){
  44. visible_data.push(data);
  45. visible_layers.push(L.geoJson(data, {
  46. style: geojsonLineOptions
  47. }).addTo(map));
  48. // If the new layer is completely outside the current view, zoom the map to fit all layers
  49. var layer = visible_layers[visible_layers.length - 1];
  50. var is_outside = false;
  51. if(!map.getBounds().intersects(layer.getBounds())) {
  52. is_outside = true;
  53. }
  54. if(is_outside) {
  55. var full_bounds;
  56. for(var i in visible_layers) {
  57. layer = visible_layers[i];
  58. if(full_bounds) {
  59. full_bounds.extend(layer.getBounds());
  60. } else {
  61. full_bounds = layer.getBounds();
  62. }
  63. }
  64. map.fitBounds(full_bounds);
  65. }
  66. });
  67. return false;
  68. });
  69. function pointFromGeoJSON(geo) {
  70. return L.latLng(geo[1], geo[0])
  71. }
  72. $('#btn-play').click(function(){
  73. console.log(visible_data[0].coordinates[0]);
  74. var point = pointFromGeoJSON(visible_data[0].coordinates[0]);
  75. resetAnimation();
  76. animatedMarker = L.marker(point);
  77. animatedMarker.addTo(map);
  78. timers = [];
  79. var interval = 3;
  80. for(var i in visible_data[0].coordinates) {
  81. (function(i){
  82. timers.push(setTimeout(function(){
  83. point = pointFromGeoJSON(visible_data[0].coordinates[i]);
  84. animatedMarker.setLatLng(point);
  85. }, interval*i));
  86. })(i);
  87. }
  88. });
  89. });