index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. var app, map;
  2. var DEFAULT_ZOOM = 15;
  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 ], 18);
  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: 18,
  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. searchMarker: {},
  48. markerDetailsOpen: false,
  49. markerDetails: {
  50. businessDetails: null
  51. }
  52. },
  53. methods: {
  54. openPlaceDetails: function (item) {
  55. var that = this;
  56. var params = {
  57. name: item.name,
  58. lat: item.lat,
  59. lon: item.lon,
  60. class: item.class
  61. };
  62. axios.get('/api/places/details', {
  63. params: params
  64. }).then(function (response) {
  65. that.markerDetailsOpen = true;
  66. that.markerDetails = item;
  67. that.markerDetails.businessDetails = response.data;
  68. }).catch(function (error) {
  69. console.error(error);
  70. that.markerDetailsOpen = true;
  71. that.markerDetails = item;
  72. });
  73. },
  74. addSearchMarker: function (items) {
  75. var that = this;
  76. that.clearSearchMarker();
  77. items.forEach(function (item) {
  78. var m = L.marker(L.latLng(item.lat, item.lon), {
  79. icon: DEFAULT_MARKER_ICON,
  80. opacity: 0.5
  81. });
  82. m.bindTooltip(item.name, { direction: 'bottom' });
  83. m.on('click', function (e) {
  84. that.openPlaceDetails(item);
  85. that.activateSearchMarker(item);
  86. });
  87. m.addTo(map);
  88. that.searchMarker[item.id] = m;
  89. });
  90. },
  91. clearSearchMarker: function () {
  92. var that = this;
  93. Object.keys(this.searchMarker).forEach(function (placeId) {
  94. that.searchMarker[placeId].remove();
  95. });
  96. this.searchMarker = {};
  97. },
  98. activateSearchMarker: function (item) {
  99. var that = this;
  100. Object.keys(this.searchMarker).forEach(function (id) {
  101. that.searchMarker[id].setOpacity(id === item.id ? 1 : 0.6);
  102. });
  103. },
  104. activateSearchItem: function (item) {
  105. map.flyTo(L.latLng(item.lat, item.lon), DEFAULT_ZOOM);
  106. this.activateSearchMarker(item);
  107. },
  108. searchPlaces: function () {
  109. var that = this;
  110. this.querySearchResults = [];
  111. this.markerDetailsOpen = false;
  112. var params = {
  113. search: this.querySearchString,
  114. lat: map.getCenter().lat,
  115. lon: map.getCenter().lng
  116. };
  117. axios.get('/api/places', {
  118. params: params
  119. }).then(function (response) {
  120. that.querySearchResults = response.data;
  121. that.sidePanelOpen = true;
  122. that.addSearchMarker(that.querySearchResults);
  123. if (that.querySearchResults[0]) that.activateSearchItem(that.querySearchResults[0]);
  124. }).catch(function (error) {
  125. console.error(error);
  126. });
  127. }
  128. },
  129. created: function () {
  130. var that = this;
  131. Vue.nextTick(function () {
  132. setupMap();
  133. document.getElementById('querySearchInput').focus();
  134. });
  135. }
  136. });