index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. var app, map;
  2. var DEFAULT_ZOOM = 17;
  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. 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. title: item.name
  82. });
  83. m.bindTooltip(item.name, { direction: 'bottom' });
  84. m.on('click', function (e) {
  85. that.openPlaceDetails(item);
  86. that.activateSearchMarker(item);
  87. });
  88. m.addTo(map);
  89. that.searchMarker[item.id] = m;
  90. });
  91. },
  92. clearSearchMarker: function () {
  93. var that = this;
  94. Object.keys(this.searchMarker).forEach(function (placeId) {
  95. that.searchMarker[placeId].remove();
  96. });
  97. this.searchMarker = {};
  98. },
  99. activateSearchMarker: function (item) {
  100. var that = this;
  101. Object.keys(this.searchMarker).forEach(function (id) {
  102. that.searchMarker[id].setOpacity(id === item.id ? 1 : 0.6);
  103. });
  104. },
  105. activateSearchItem: function (item) {
  106. this.querySearchString = item.displayName;
  107. map.flyTo(L.latLng(item.lat, item.lon), DEFAULT_ZOOM);
  108. this.activateSearchMarker(item);
  109. },
  110. searchPlaces: function () {
  111. var that = this;
  112. this.querySearchResults = [];
  113. this.markerDetailsOpen = false;
  114. var params = {
  115. search: this.querySearchString,
  116. lat: map.getCenter().lat,
  117. lon: map.getCenter().lng
  118. };
  119. axios.get('/api/places', {
  120. params: params
  121. }).then(function (response) {
  122. that.querySearchResults = response.data;
  123. that.sidePanelOpen = true;
  124. that.addSearchMarker(that.querySearchResults);
  125. if (that.querySearchResults[0]) that.activateSearchItem(that.querySearchResults[0]);
  126. }).catch(function (error) {
  127. console.error(error);
  128. });
  129. }
  130. },
  131. created: function () {
  132. var that = this;
  133. Vue.nextTick(function () {
  134. setupMap();
  135. document.getElementById('querySearchInput').focus();
  136. });
  137. }
  138. });