wowchemy-map.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*************************************************
  2. * Wowchemy
  3. * https://github.com/wowchemy/wowchemy-hugo-themes
  4. *
  5. * Wowchemy Maps
  6. **************************************************/
  7. // Initialize Google Maps or OpenStreetMap via Leaflet.
  8. function initMap() {
  9. if ($('#map').length) {
  10. let map_provider = $('#map-provider').val();
  11. let lat = $('#map-lat').val();
  12. let lng = $('#map-lng').val();
  13. let zoom = parseInt($('#map-zoom').val());
  14. let address = $('#map-dir').val();
  15. let api_key = $('#map-api-key').val();
  16. if (map_provider === 'google') {
  17. let map = new GMaps({
  18. div: '#map',
  19. lat: lat,
  20. lng: lng,
  21. zoom: zoom,
  22. zoomControl: true,
  23. zoomControlOpt: {
  24. style: 'SMALL',
  25. position: 'TOP_LEFT',
  26. },
  27. streetViewControl: false,
  28. mapTypeControl: false,
  29. gestureHandling: 'cooperative',
  30. });
  31. map.addMarker({
  32. lat: lat,
  33. lng: lng,
  34. click: function () {
  35. let url = 'https://www.google.com/maps/place/' + encodeURIComponent(address) + '/@' + lat + ',' + lng + '/';
  36. window.open(url, '_blank');
  37. },
  38. title: address,
  39. });
  40. } else {
  41. let map = new L.map('map').setView([lat, lng], zoom);
  42. if (map_provider === 'mapbox' && api_key.length) {
  43. L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
  44. attribution:
  45. 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
  46. tileSize: 512,
  47. maxZoom: 18,
  48. zoomOffset: -1,
  49. id: 'mapbox/streets-v11',
  50. accessToken: api_key,
  51. }).addTo(map);
  52. } else {
  53. L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  54. maxZoom: 19,
  55. attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
  56. }).addTo(map);
  57. }
  58. let marker = L.marker([lat, lng]).addTo(map);
  59. let url = lat + ',' + lng + '#map=' + zoom + '/' + lat + '/' + lng + '&layers=N';
  60. marker.bindPopup(
  61. address +
  62. '<p><a href="https://www.openstreetmap.org/directions?engine=osrm_car&route=' +
  63. url +
  64. '">Routing via OpenStreetMap</a></p>',
  65. );
  66. }
  67. }
  68. }
  69. document.addEventListener('DOMContentLoaded', function () {
  70. // Initialise street maps if necessary.
  71. initMap();
  72. });