index.js 4.5 KB

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