index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // attempt to set a closer default
  26. navigator.geolocation.getCurrentPosition(function (position) {
  27. map.jumpTo({ center: [ position.coords.longitude, position.coords.latitude ], zoom: 12 });
  28. });
  29. }
  30. app = new Vue({
  31. el: '#app',
  32. data: {
  33. sidePanelOpen: false,
  34. directionsOpen: false,
  35. position: null,
  36. querySearchString: '',
  37. querySearchStringTo: '',
  38. querySearchResults: [],
  39. searchPositionMarker: null,
  40. markerDetailsOpen: false,
  41. markerDetails: {
  42. businessDetails: null
  43. }
  44. },
  45. methods: {
  46. getPositionDetails: function () {
  47. var that = this;
  48. navigator.geolocation.getCurrentPosition(function (position) {
  49. axios.get('https://nominatim.openstreetmap.org/reverse?lat=52&lon=13', {
  50. params: {
  51. format: 'json',
  52. lat: position.coords.latitude,
  53. lon: position.coords.longitude,
  54. }
  55. }).then(function (response) {
  56. that.position = {
  57. coords: position.coords,
  58. details: response.data
  59. };
  60. }).catch(function (error) {
  61. console.error('Unable to get location details:', error);
  62. that.position = {
  63. coords: position.coords,
  64. details: null
  65. };
  66. });
  67. });
  68. },
  69. searchMarkerClicked: function (item) {
  70. var that = this;
  71. var params = {
  72. name: item.name,
  73. lat: item.lat,
  74. lon: item.lon
  75. };
  76. axios.get('/api/places/details', {
  77. params: params
  78. }).then(function (response) {
  79. that.markerDetailsOpen = true;
  80. that.markerDetails = item;
  81. that.markerDetails.businessDetails = response.data;
  82. }).catch(function (error) {
  83. console.error(error);
  84. that.markerDetailsOpen = true;
  85. that.markerDetails = item;
  86. });
  87. },
  88. setSearchPositionMarker: function (item) {
  89. var that = this;
  90. if (this.searchPositionMarker) this.searchPositionMarker.remove();
  91. var el = document.createElement('div');
  92. el.className = 'search-result-marker';
  93. el.addEventListener('click', function (e) {
  94. that.searchMarkerClicked(item);
  95. });
  96. this.searchPositionMarker = new mapboxgl.Marker(el);
  97. this.searchPositionMarker.setLngLat([ item.lon, item.lat ]);
  98. this.searchPositionMarker.addTo(map);
  99. map.flyTo({ center: [ item.lon, item.lat ], zoom: 16 });
  100. },
  101. searchSelectItem: function (item) {
  102. this.querySearchString = item.display_name;
  103. this.setSearchPositionMarker(item);
  104. },
  105. doQuerySearch: function () {
  106. var that = this;
  107. this.querySearchResults = [];
  108. this.markerDetailsOpen = false;
  109. var params = {
  110. search: queryString
  111. };
  112. if (position && position.coords) {
  113. params.lat = position.coords.latitude;
  114. params.lon = position.coords.longitude;
  115. }
  116. axios.get('/api/places', {
  117. params: params
  118. }).then(function (response) {
  119. that.querySearchResults = response.data;
  120. that.sidePanelOpen = true;
  121. if (that.querySearchResults[0]) that.setSearchPositionMarker(that.querySearchResults[0]);
  122. }).catch(function (error) {
  123. console.error(error);
  124. });
  125. }
  126. },
  127. created: function () {
  128. var that = this;
  129. Vue.nextTick(function () {
  130. setupMap();
  131. that.getPositionDetails();
  132. document.getElementById('querySearchInput').focus();
  133. });
  134. }
  135. });