index.js 4.5 KB

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