index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. mapboxgl.accessToken = 'pk.eyJ1IjoibmVidWxvbiIsImEiOiI3NmNkODc5ZDMyNjcxMWM2M2UyODEyMjdkM2YyMjVlOSJ9.Cpz0-WRwBwE-DNdNfGDu_w';
  2. var app, map;
  3. function setupMap() {
  4. map = new mapboxgl.Map({
  5. container: 'map',
  6. // style: 'mapbox://styles/nebulon/cjpa4rr8f0bjw2sruosd5r196',
  7. style: 'mapbox://styles/nebulon/cjpmm73gq0fbg2speu42o2y8a', // with railway stations
  8. center: [ 13.404954, 52.520008 ], // Berlin
  9. zoom: 5
  10. });
  11. var scale = new mapboxgl.ScaleControl({
  12. maxWidth: 80,
  13. unit: 'metric'
  14. });
  15. map.addControl(scale, 'bottom-right');
  16. const locationControl = new mapboxgl.GeolocateControl({
  17. positionOptions: {
  18. enableHighAccuracy: true
  19. },
  20. trackUserLocation: true
  21. });
  22. map.addControl(locationControl);
  23. var nav = new mapboxgl.NavigationControl();
  24. map.addControl(nav, 'top-right');
  25. // set map position either based on url or default
  26. var u = new URL(window.location);
  27. var lon = u.searchParams.get('lon');
  28. var lat = u.searchParams.get('lat');
  29. var zoom = u.searchParams.get('zoom');
  30. if (lon && lat) {
  31. map.jumpTo({ center: [ lon, lat ], zoom: zoom || 12 });
  32. } else {
  33. navigator.geolocation.getCurrentPosition(function (position) {
  34. map.jumpTo({ center: [ position.coords.longitude, position.coords.latitude ], zoom: 12 });
  35. });
  36. }
  37. // Update the url bar to match position
  38. map.on('moveend', function (event) {
  39. var u = new URL(window.location);
  40. u.searchParams.set('lon', map.getCenter().lng);
  41. u.searchParams.set('lat', map.getCenter().lat);
  42. u.searchParams.set('zoom', map.getZoom());
  43. history.replaceState({}, window.document.title, u);
  44. });
  45. }
  46. app = new Vue({
  47. el: '#app',
  48. data: {
  49. sidePanelOpen: false,
  50. directionsOpen: false,
  51. position: null,
  52. querySearchString: '',
  53. querySearchStringTo: '',
  54. querySearchResults: [],
  55. searchPositionMarker: null,
  56. markerDetailsOpen: false,
  57. markerDetails: {
  58. businessDetails: null
  59. }
  60. },
  61. methods: {
  62. getPositionDetails: function () {
  63. var that = this;
  64. navigator.geolocation.getCurrentPosition(function (position) {
  65. axios.get('https://nominatim.openstreetmap.org/reverse?lat=52&lon=13', {
  66. params: {
  67. format: 'json',
  68. lat: position.coords.latitude,
  69. lon: position.coords.longitude,
  70. }
  71. }).then(function (response) {
  72. that.position = {
  73. coords: position.coords,
  74. details: response.data
  75. };
  76. }).catch(function (error) {
  77. console.error('Unable to get location details:', error);
  78. that.position = {
  79. coords: position.coords,
  80. details: null
  81. };
  82. });
  83. });
  84. },
  85. searchMarkerClicked: function (item) {
  86. var that = this;
  87. var params = {
  88. name: item.name,
  89. lat: item.lat,
  90. lon: item.lon
  91. };
  92. axios.get('/api/places/details', {
  93. params: params
  94. }).then(function (response) {
  95. that.markerDetailsOpen = true;
  96. that.markerDetails = item;
  97. that.markerDetails.businessDetails = response.data;
  98. }).catch(function (error) {
  99. console.error(error);
  100. that.markerDetailsOpen = true;
  101. that.markerDetails = item;
  102. });
  103. },
  104. setSearchPositionMarker: function (item) {
  105. var that = this;
  106. if (this.searchPositionMarker) this.searchPositionMarker.remove();
  107. var el = document.createElement('div');
  108. el.className = 'search-result-marker';
  109. el.addEventListener('click', function (e) {
  110. that.searchMarkerClicked(item);
  111. });
  112. this.searchPositionMarker = new mapboxgl.Marker(el);
  113. this.searchPositionMarker.setLngLat([ item.lon, item.lat ]);
  114. this.searchPositionMarker.addTo(map);
  115. map.flyTo({ center: [ item.lon, item.lat ], zoom: 16 });
  116. },
  117. searchSelectItem: function (item) {
  118. this.querySearchString = item.display_name;
  119. this.setSearchPositionMarker(item);
  120. },
  121. doQuerySearch: function () {
  122. var that = this;
  123. this.querySearchResults = [];
  124. this.markerDetailsOpen = false;
  125. var params = {
  126. search: this.querySearchString,
  127. lat: map.getCenter().lat,
  128. lon: map.getCenter().lng
  129. };
  130. axios.get('/api/places', {
  131. params: params
  132. }).then(function (response) {
  133. that.querySearchResults = response.data;
  134. that.sidePanelOpen = true;
  135. if (that.querySearchResults[0]) that.setSearchPositionMarker(that.querySearchResults[0]);
  136. }).catch(function (error) {
  137. console.error(error);
  138. });
  139. }
  140. },
  141. created: function () {
  142. var that = this;
  143. Vue.nextTick(function () {
  144. setupMap();
  145. that.getPositionDetails();
  146. document.getElementById('querySearchInput').focus();
  147. });
  148. }
  149. });