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.

94 lines
2.4 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. function getCurrentLocation() {
  31. var interval = 5000;
  32. if(getQueryVariable('interval')) {
  33. interval = getQueryVariable('interval');
  34. }
  35. $.getJSON("/share/current.json?token="+$("#share_token").val(), function(data){
  36. if(data.data) {
  37. moveMarkerToPosition(data.data);
  38. map.setView(currentLocationMarker.getLatLng());
  39. }
  40. setTimeout(getCurrentLocation, interval);
  41. });
  42. }
  43. getCurrentLocation();
  44. function moveMarkerToPosition(feature) {
  45. if(feature && feature.geometry) {
  46. var coord = pointFromGeoJSON(feature.geometry.coordinates);
  47. if(coord) {
  48. if(!currentTrack) {
  49. currentTrack = L.polyline([coord, coord]).addTo(map);
  50. } else {
  51. currentTrack.addLatLng(coord);
  52. }
  53. if(!currentLocationMarker) {
  54. currentLocationMarker = L.marker(coord).addTo(map);
  55. } else {
  56. currentLocationMarker.setLatLng(coord);
  57. }
  58. }
  59. }
  60. }
  61. function pointFromGeoJSON(geo) {
  62. return L.latLng(geo[1], geo[0])
  63. }
  64. function getQueryVariable(variable) {
  65. var query = window.location.search.substring(1);
  66. var vars = query.split('&');
  67. for (var i = 0; i < vars.length; i++) {
  68. var pair = vars[i].split('=');
  69. if (decodeURIComponent(pair[0]) == variable) {
  70. return decodeURIComponent(pair[1]);
  71. }
  72. }
  73. }