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.

108 lines
2.8 KiB

  1. var map = L.map('map', { zoomControl: false }).setView([45.516, -122.660], 14, null, null, 24);
  2. var opts = {
  3. attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
  4. maxZoom: 18,
  5. zoomOffset: -1,
  6. tileSize: 512,
  7. id: 'mapbox/light-v10',
  8. accessToken: 'pk.eyJ1IjoiYWFyb25wayIsImEiOiJja3A0eXV2ZXIwMGt3MnVuc2Uzcm1yYzFuIn0.-_qwPOLRiQk8t56xs6vkfg'
  9. };
  10. if(getQueryVariable('attribution') == 0) {
  11. opts.attribution = 'Mapbox';
  12. }
  13. L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', opts).addTo(map);
  14. if(getQueryVariable('controls') != 0) {
  15. new L.Control.Zoom({ position: 'topleft' }).addTo(map);
  16. }
  17. var geojsonLineOptions = {
  18. color: "#0033ff",
  19. weight: 4,
  20. opacity: 0.5
  21. };
  22. var startIcon = L.icon({
  23. iconUrl: '/assets/map-pin-start.png',
  24. iconSize: [18,28],
  25. iconAnchor: [9,28]
  26. });
  27. // Load the current location and show on the map
  28. var currentLocationMarker;
  29. var currentTrack;
  30. var lastSeenTimestamp;
  31. function getCurrentLocation() {
  32. var interval = 5000;
  33. if(getQueryVariable('interval')) {
  34. interval = getQueryVariable('interval');
  35. }
  36. $.getJSON("/share/current.json?token="+$("#share_token").val(), function(data){
  37. if(data.data) {
  38. moveMarkerToPosition(data.data);
  39. if(lastSeenTimestamp != data.data.properties.timestamp) {
  40. map.setView(currentLocationMarker.getLatLng());
  41. }
  42. lastSeenTimestamp = data.data.properties.timestamp;
  43. }
  44. setTimeout(getCurrentLocation, interval);
  45. });
  46. }
  47. function getRecentHistory() {
  48. $.getJSON("/share/history.json?token="+$("#share_token").val(), function(data){
  49. if(data.linestring) {
  50. L.geoJson(data.linestring, {
  51. style: geojsonLineOptions
  52. }).addTo(map);
  53. }
  54. });
  55. }
  56. getCurrentLocation();
  57. getRecentHistory();
  58. function moveMarkerToPosition(feature) {
  59. if(feature && feature.geometry) {
  60. var coord = pointFromGeoJSON(feature.geometry.coordinates);
  61. if(coord) {
  62. if(!currentTrack) {
  63. currentTrack = L.polyline([coord, coord]).addTo(map);
  64. } else {
  65. currentTrack.addLatLng(coord);
  66. }
  67. if(!currentLocationMarker) {
  68. currentLocationMarker = L.marker(coord).addTo(map);
  69. } else {
  70. currentLocationMarker.setLatLng(coord);
  71. }
  72. }
  73. }
  74. }
  75. function pointFromGeoJSON(geo) {
  76. return L.latLng(geo[1], geo[0])
  77. }
  78. function getQueryVariable(variable) {
  79. var query = window.location.search.substring(1);
  80. var vars = query.split('&');
  81. for (var i = 0; i < vars.length; i++) {
  82. var pair = vars[i].split('=');
  83. if (decodeURIComponent(pair[0]) == variable) {
  84. return decodeURIComponent(pair[1]);
  85. }
  86. }
  87. }