map.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var longitude = -95.125;
  2. var latitude = 40.596;
  3. var zoom = 5;
  4. // var mymap = L.map('mapid').setView([latitude, longitude], zoom);
  5. var accessToken = "pk.eyJ1Ijoiam9yZGFuZGFzaGVsIiwiYSI6ImNqdWQ3OHlneTBsMms0NG82YTVkMmg3dHgifQ.NmgYJNL8FVwq-gctstiAVA"
  6. // L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
  7. // 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>',
  8. // maxZoom: 18,
  9. // id: 'mapbox.streets',
  10. // accessToken: accessToken
  11. // }).addTo(mymap);
  12. mapboxgl.accessToken = accessToken;
  13. var mymap = new mapboxgl.Map({
  14. container: 'mapid',
  15. style: 'mapbox://styles/mapbox/streets-v11',
  16. center: [longitude, latitude],
  17. zoom: 3
  18. });
  19. function addPin(town) {
  20. town = town.replace(/u'/g, "'");
  21. town = town.replace(/'/g, '"');
  22. town = JSON.parse(town);
  23. latitude = town.coordinates.lat;
  24. longitude = town.coordinates.lon;
  25. city = town.city;
  26. anchor = city.split(',')[0].replace(' ', '');
  27. htmlContent = "<a href='/stops#" + anchor + "'>";
  28. htmlContent += "<h1>" + city + "</h1><br/>";
  29. if (town.photos) {
  30. htmlContent += "<img class='townphoto' src='/static/photos/" + town.photos[0].filename + "'>";
  31. }
  32. htmlContent += "</a>";
  33. var pin = new mapboxgl.Marker()
  34. .setLngLat([longitude, latitude])
  35. .setPopup(new mapboxgl.Popup().setMaxWidth("none").setHTML(htmlContent))
  36. .addTo(mymap);
  37. // var pin = L.marker([latitude, longitude]).addTo(mymap);
  38. // pin.bindPopup(htmlContent, {maxWidth: "auto"});
  39. // This is a hack to center the pointers above the pins.
  40. // When you first open a pin, the triangle pointing to
  41. // the pin is centered to the image, nowhere near the
  42. // pin. If you close and reopen -- manually OR
  43. // automatically -- it will reset the marker to where
  44. // it should be. These two functions are a workaround.
  45. // pin.openPopup();
  46. // mymap.closePopup();
  47. }
  48. function resizePins() {
  49. // Popups were not resizing to fit images. I could not figure it out.
  50. // This drove me nuts; I think it's exposing my webdev ignorance.
  51. // Solution credit: https://stackoverflow.com/questions/38170366/leaflet-adjust-popup-to-picture-size
  52. document.querySelector(".leaflet-popup-pane").addEventListener("load", function (event) {
  53. var tagName = event.target.tagName,
  54. popup = mymap._popup; // Currently open popup, if any.
  55. if (tagName === "IMG" && popup) {
  56. popup.update();
  57. }
  58. }, true); // Capture the load event, because it does not bubble.
  59. }
  60. function recenterMap() {
  61. var longitude = -95.125;
  62. var latitude = 40.596;
  63. zoom = 5;
  64. mymap.setView([latitude, longitude], zoom);
  65. }