index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. querySearchString: '',
  52. querySearchStringTo: '',
  53. querySearchResults: [],
  54. searchPositionMarker: null,
  55. searchMarker: [],
  56. markerDetailsOpen: false,
  57. markerDetails: {
  58. businessDetails: null
  59. }
  60. },
  61. methods: {
  62. openPlaceDetails: function (item) {
  63. var that = this;
  64. var params = {
  65. name: item.name,
  66. lat: item.lat,
  67. lon: item.lon,
  68. class: item.class
  69. };
  70. axios.get('/api/places/details', {
  71. params: params
  72. }).then(function (response) {
  73. that.markerDetailsOpen = true;
  74. that.markerDetails = item;
  75. that.markerDetails.businessDetails = response.data;
  76. }).catch(function (error) {
  77. console.error(error);
  78. that.markerDetailsOpen = true;
  79. that.markerDetails = item;
  80. });
  81. },
  82. addSearchMarker: function (items) {
  83. var that = this;
  84. that.clearSearchMarker();
  85. items.forEach(function (item) {
  86. var el = document.createElement('div');
  87. el.className = 'search-result-marker';
  88. el.addEventListener('click', function (e) {
  89. that.openPlaceDetails(item);
  90. });
  91. var m = new mapboxgl.Marker({ element: el });
  92. m.setLngLat([ item.lon, item.lat ]);
  93. m.addTo(map);
  94. that.searchMarker.push(m);
  95. });
  96. },
  97. clearSearchMarker: function () {
  98. this.searchMarker.forEach(function (marker) {
  99. marker.remove();
  100. });
  101. this.searchMarker = [];
  102. },
  103. activateSearchItem: function (item) {
  104. this.querySearchString = item.display_name;
  105. map.flyTo({ center: [ item.lon, item.lat ], zoom: 16 });
  106. },
  107. searchPlaces: function () {
  108. var that = this;
  109. this.querySearchResults = [];
  110. this.markerDetailsOpen = false;
  111. var params = {
  112. search: this.querySearchString,
  113. lat: map.getCenter().lat,
  114. lon: map.getCenter().lng
  115. };
  116. axios.get('/api/places', {
  117. params: params
  118. }).then(function (response) {
  119. that.querySearchResults = response.data;
  120. that.sidePanelOpen = true;
  121. that.addSearchMarker(that.querySearchResults.slice(0,1));
  122. if (that.querySearchResults[0]) map.flyTo({ center: [ that.querySearchResults[0].lon, that.querySearchResults[0].lat ], zoom: 16 });
  123. }).catch(function (error) {
  124. console.error(error);
  125. });
  126. }
  127. },
  128. created: function () {
  129. var that = this;
  130. Vue.nextTick(function () {
  131. setupMap();
  132. document.getElementById('querySearchInput').focus();
  133. });
  134. }
  135. });