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.

234 lines
7.8 KiB

  1. /* eslint-disable */
  2. var map = L.map('mapid').setView([63.8333, -152], 1),
  3. accessToken = 'pk.eyJ1IjoiZmx5bG9jYWwiLCJhIjoiY2t0OHUxZXB6MTVueTJ4cGVwOHRuc2s2NyJ9.YF9frLvISHfOuT7nqs3TNg',
  4. apiUrl = `https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=${accessToken}`;
  5. var form = `AND(Is_Origin=1,{IsCurrent-AsOrigin}='Yes')`;
  6. var markers = L.markerClusterGroup();
  7. var destinationsLayer = L.layerGroup();
  8. var points = [],
  9. mainOffset;
  10. async function fetchMapData() {
  11. const response = await fetch(`https://api.airtable.com/v0/appiQwfVZixRgRICe/Airports_IATA?filterByFormula=${form}&fields[]=Airport_IATA&fields[]=Icon_Url&fields[]=Latitude_Deg&fields[]=Longitude_Deg&sort[0][field]=Airport_IATA&sort[0][direction]=asc`, {
  12. method: 'GET',
  13. headers: {
  14. 'Content-Type': 'application/x-www-form-urlencoded',
  15. 'Authorization': 'Bearer keyJ2ht64ZSN57AG1'
  16. },
  17. });
  18. response.ok; // => false
  19. response.status; // => 404
  20. const mapData = await response.json();
  21. points = mapData.records;
  22. mainOffset = mapData.offset;
  23. return mapData;
  24. }
  25. async function fetchMapData2(mapData) {
  26. if (mapData.offset) {
  27. const response = await fetch(`https://api.airtable.com/v0/appiQwfVZixRgRICe/Airports_IATA?filterByFormula=${form}&fields[]=Airport_IATA&fields[]=Icon_Url&fields[]=Latitude_Deg&fields[]=Longitude_Deg&sort[0][field]=Airport_IATA&sort[0][direction]=asc&offset=${mapData.offset}`, {
  28. method: 'GET',
  29. headers: {
  30. 'Content-Type': 'application/x-www-form-urlencoded',
  31. 'Authorization': 'Bearer keyJ2ht64ZSN57AG1'
  32. },
  33. });
  34. response.ok; // => false
  35. response.status; // => 404
  36. const mapData2 = await response.json();
  37. points = [...points, ...mapData2.records];
  38. return mapData2;
  39. }
  40. }
  41. function getOrigIcon(icon) {
  42. var origIcon = L.icon({
  43. iconUrl: icon,
  44. //shadowUrl: 'leaf-shadow.png',
  45. iconSize: [80, 40], // size of the icon
  46. //shadowSize: [50, 64], // size of the shadow
  47. iconAnchor: [40, 40], // point of the icon which will correspond to marker's location
  48. //shadowAnchor: [4, 62], // the same for the shadow
  49. popupAnchor: [0, 0] // point from which the popup should open relative to the iconAnchor
  50. });
  51. return origIcon;
  52. }
  53. function getBlueIcon() {
  54. var origIcon = L.icon({
  55. iconUrl: 'https://spencerflagg.com/flylocal/pins/pin.svg',
  56. //shadowUrl: 'leaf-shadow.png',
  57. iconSize: [14, 16], // size of the icon
  58. //shadowSize: [50, 64], // size of the shadow
  59. iconAnchor: [7, 16], // point of the icon which will correspond to marker's location
  60. //shadowAnchor: [4, 62], // the same for the shadow
  61. popupAnchor: [0, 0] // point from which the popup should open relative to the iconAnchor
  62. });
  63. return origIcon;
  64. }
  65. fetchMapData().then(mapData => {
  66. fetchMapData2(mapData).then(function () {
  67. var mapDataFiltered = points.map((record) => {
  68. return {
  69. "iata": record.fields.Airport_IATA,
  70. "lat": record.fields.Latitude_Deg,
  71. "long": record.fields.Longitude_Deg,
  72. "icon": record.fields.Icon_Url
  73. }
  74. });
  75. // console.log(mapDataFiltered); // => 'Page not found'
  76. mapDataFiltered.forEach((record) => {
  77. var popupContent = `
  78. <div>${record.iata}</div>
  79. <button id='origBtn' data-iata='${record.iata}' data-lat='${record.lat}' data-long='${record.long}'>make origin</button>
  80. `;
  81. var marker = L.marker([record.lat, record.long], { icon: getOrigIcon(record.icon + '+FFFFFF+BBBBBB+000000') })
  82. .bindPopup(popupContent);
  83. //.addTo(map);
  84. markers.addLayer(marker);
  85. });
  86. })
  87. });
  88. // console.log(mapData[0].Airport_IATA);
  89. L.tileLayer(apiUrl, {
  90. attribution: 'FlyLocal | Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
  91. maxZoom: 18,
  92. //id: 'mapbox/streets-v11',
  93. //id: 'mapbox/outdoors-v11',
  94. //id: 'mapbox/satellite-streets-v11',
  95. id: 'flylocal/ckt9x8aho0igb18mmm9ks2fsv',
  96. tileSize: 512,
  97. zoomOffset: -1,
  98. accessToken: 'your.mapbox.access.token'
  99. }).addTo(map);
  100. map.addLayer(markers);
  101. map.on('popupopen', function (e) {
  102. var thisMarker = e.popup._source;
  103. // console.log(thisMarker);
  104. var origButton = document.getElementById("origBtn"),
  105. destButton = document.getElementById("destBtn");
  106. origButton.addEventListener("click", function () {
  107. //alert(thisButton.dataset.iata);
  108. fetchDestData(origButton.dataset.iata).then(mapData => {
  109. var mapDataFiltered = mapData.records.map((record) => {
  110. return {
  111. "iata": record.fields.Airport_IATA,
  112. "lat": record.fields.Latitude_Deg,
  113. "long": record.fields.Longitude_Deg,
  114. "icon": record.fields.Icon_Url
  115. }
  116. });
  117. // console.log(mapDataFiltered); // => 'Page not found'
  118. map.removeLayer(markers);
  119. // re-add origin
  120. var marker = L.marker([thisButton.dataset.lat, thisButton.dataset.long], { icon: getBlueIcon() })
  121. .addTo(map);
  122. destinationsLayer.clearLayers();
  123. map.removeLayer(destinationsLayer);
  124. // add all destinations
  125. mapDataFiltered.forEach((record) => {
  126. var popupContent = `
  127. <div>${record.iata}</div>
  128. <button id='origBtn' data-iata='${record.iata}' data-lat='${record.lat}' data-long='${record.long}'>make origin</button>
  129. <button id='destBtn' data-iata='${record.iata}' data-lat='${record.lat}' data-long='${record.long}'>make destination</button>
  130. `;
  131. var marker = L.marker([record.lat, record.long], { icon: getOrigIcon(record.icon + '+05FF00+04C300+000000') })
  132. .bindPopup(popupContent).addTo(map);
  133. });
  134. map.addLayer(destinationsLayer);
  135. });
  136. });
  137. destButton.addEventListener("click", function () {
  138. var lat = origButton.dataset.lat,
  139. long = origButton.dataset.long;
  140. var thisArc = L.Polyline.Arc([thisButton.dataset.lat, thisButton.dataset.long], [record.lat, record.long], {
  141. vertices: 200,
  142. });
  143. var thisDecorator = L.polylineDecorator(thisArc, {
  144. patterns: [
  145. // defines a pattern of 10px-wide dashes, repeated every 20px on the line
  146. { offset: 0, repeat: 20, symbol: L.Symbol.dash({ pixelSize: 3, pathOptions: { color: '#007FFF', weight: 8, lineCap: 'square' } }) },
  147. { offset: '100%', repeat: 0, symbol: L.Symbol.arrowHead({ pixelSize: 15, polygon: false, pathOptions: { stroke: true, color: '#007FFF', weight: 8, lineCap: 'square' } }) }
  148. ]
  149. });
  150. destinationsLayer.addLayer(thisDecorator);
  151. });
  152. });
  153. async function fetchDestData(iata) {
  154. var destFormula = `AND(Is_Destination=1,{IsCurrent-AsDest}="Yes",(FIND("${iata}", Associated_Origin_Search)>0))`;
  155. const response = await fetch(`https://api.airtable.com/v0/appiQwfVZixRgRICe/Airports_IATA?filterByFormula=${destFormula}&fields[]=Airport_IATA&fields[]=Latitude_Deg&fields[]=Longitude_Deg&fields[]=Icon_Url&sort[0][field]=Airport_IATA&sort[0][direction]=asc`, {
  156. method: 'GET',
  157. headers: {
  158. 'Content-Type': 'application/x-www-form-urlencoded',
  159. 'Authorization': 'Bearer keyJ2ht64ZSN57AG1'
  160. },
  161. });
  162. response.ok; // => false
  163. response.status; // => 404
  164. const mapData = await response.json();
  165. points = mapData.records;
  166. mainOffset = mapData.offset;
  167. return mapData;
  168. }