index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. var app, map;
  2. var DEFAULT_ZOOM = 18;
  3. var DEFAULT_MARKER_ICON = L.icon({
  4. iconUrl: 'pin.svg',
  5. iconSize: [80, 80]
  6. });
  7. function setupMap() {
  8. map = new L.map('map', { zoomControl: false });
  9. map.setView([ 52.520008, 13.404954 ], 15);
  10. L.control.scale({ position: 'bottomright', imperial: false }).addTo(map);
  11. L.control.zoom({ position: 'topright' }).addTo(map);
  12. L.tileLayer('https://api.mapbox.com/styles/v1/nebulon/{id}/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoibmVidWxvbiIsImEiOiI3NmNkODc5ZDMyNjcxMWM2M2UyODEyMjdkM2YyMjVlOSJ9.Cpz0-WRwBwE-DNdNfGDu_w', {
  13. 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>',
  14. maxZoom: DEFAULT_ZOOM,
  15. id: 'cjpmm73gq0fbg2speu42o2y8a',
  16. accessToken: 'pk.eyJ1IjoibmVidWxvbiIsImEiOiI3NmNkODc5ZDMyNjcxMWM2M2UyODEyMjdkM2YyMjVlOSJ9.Cpz0-WRwBwE-DNdNfGDu_w'
  17. }).addTo(map);
  18. // set map position either based on url or default
  19. var u = new URL(window.location);
  20. var lon = u.searchParams.get('lon');
  21. var lat = u.searchParams.get('lat');
  22. var zoom = u.searchParams.get('zoom');
  23. if (lon && lat) {
  24. map.flyTo(L.latLng(lat, lon), zoom || DEFAULT_ZOOM);
  25. } else {
  26. navigator.geolocation.getCurrentPosition(function (pos) {
  27. map.flyTo(L.latLng(pos.coords.latitude, pos.coords.longitude), DEFAULT_ZOOM);
  28. });
  29. }
  30. // Update the url bar to match position
  31. map.on('moveend', function (event) {
  32. var u = new URL(window.location);
  33. u.searchParams.set('lon', map.getCenter().lng);
  34. u.searchParams.set('lat', map.getCenter().lat);
  35. u.searchParams.set('zoom', map.getZoom());
  36. history.replaceState({}, window.document.title, u);
  37. });
  38. }
  39. app = new Vue({
  40. el: '#app',
  41. data: {
  42. sidePanelOpen: false,
  43. directionsOpen: false,
  44. querySearchString: '',
  45. querySearchStringTo: '',
  46. querySearchResults: [],
  47. searchPositionMarker: null,
  48. searchMarker: {},
  49. markerDetailsOpen: false,
  50. markerDetails: {
  51. businessDetails: null
  52. }
  53. },
  54. methods: {
  55. openPlaceDetails: function (item) {
  56. var that = this;
  57. var params = {
  58. name: item.name,
  59. lat: item.lat,
  60. lon: item.lon,
  61. class: item.class
  62. };
  63. axios.get('/api/places/details', {
  64. params: params
  65. }).then(function (response) {
  66. that.markerDetailsOpen = true;
  67. that.markerDetails = item;
  68. that.markerDetails.businessDetails = response.data;
  69. }).catch(function (error) {
  70. console.error(error);
  71. that.markerDetailsOpen = true;
  72. that.markerDetails = item;
  73. });
  74. },
  75. addSearchMarker: function (items) {
  76. var that = this;
  77. that.clearSearchMarker();
  78. items.forEach(function (item) {
  79. var m = L.marker(L.latLng(item.lat, item.lon), { icon: DEFAULT_MARKER_ICON, opacity: 0.5 });
  80. m.on('click', function (e) {
  81. that.openPlaceDetails(item);
  82. that.activateSearchMarker(item);
  83. });
  84. m.addTo(map);
  85. that.searchMarker[item.id] = m;
  86. });
  87. },
  88. clearSearchMarker: function () {
  89. var that = this;
  90. Object.keys(this.searchMarker).forEach(function (placeId) {
  91. that.searchMarker[placeId].remove();
  92. });
  93. this.searchMarker = {};
  94. },
  95. activateSearchMarker: function (item) {
  96. var that = this;
  97. Object.keys(this.searchMarker).forEach(function (id) {
  98. that.searchMarker[id].setOpacity(id === item.id ? 1 : 0.6);
  99. });
  100. },
  101. activateSearchItem: function (item) {
  102. this.querySearchString = item.displayName;
  103. map.flyTo(L.latLng(item.lat, item.lon), DEFAULT_ZOOM);
  104. this.activateSearchMarker(item);
  105. },
  106. searchPlaces: function () {
  107. var that = this;
  108. this.querySearchResults = [];
  109. this.markerDetailsOpen = false;
  110. var params = {
  111. search: this.querySearchString,
  112. lat: map.getCenter().lat,
  113. lon: map.getCenter().lng
  114. };
  115. axios.get('/api/places', {
  116. params: params
  117. }).then(function (response) {
  118. that.querySearchResults = response.data;
  119. that.sidePanelOpen = true;
  120. that.addSearchMarker(that.querySearchResults);
  121. if (that.querySearchResults[0]) that.activateSearchItem(that.querySearchResults[0]);
  122. }).catch(function (error) {
  123. console.error(error);
  124. });
  125. }
  126. },
  127. created: function () {
  128. var that = this;
  129. Vue.nextTick(function () {
  130. setupMap();
  131. document.getElementById('querySearchInput').focus();
  132. });
  133. }
  134. });