index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. mapboxgl.accessToken = 'pk.eyJ1IjoibmVidWxvbiIsImEiOiI3NmNkODc5ZDMyNjcxMWM2M2UyODEyMjdkM2YyMjVlOSJ9.Cpz0-WRwBwE-DNdNfGDu_w';
  2. var app, map;
  3. function setupMap() {
  4. navigator.geolocation.getCurrentPosition(function (position) {
  5. map = new mapboxgl.Map({
  6. container: 'map',
  7. style: 'mapbox://styles/nebulon/cjpa4rr8f0bjw2sruosd5r196',
  8. center: [ position.coords.longitude, position.coords.latitude ],
  9. zoom: 12.0
  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. });
  26. }
  27. function findResults(queryString, position, callback) {
  28. function amendDetails(results) {
  29. results.forEach(function (r) {
  30. // icons
  31. r.icon = 'fa-address-card';
  32. if (r.type === 'restaurant') r.icon = 'fa-utensils';
  33. if (r.type === 'doctors') r.icon = 'fa-user-md';
  34. if (r.type === 'dentist') r.icon = 'fa-tooth';
  35. if (r.type === 'car') r.icon = 'fa-car';
  36. if (r.type === 'bicycle-rental') r.icon = 'fa-bicycle';
  37. if (r.type === 'school') r.icon = 'fa-graduation-cap';
  38. if (r.type === 'canal' || r.type === 'river' || r.type === 'water') r.icon = 'fa-water';
  39. if (r.type === 'station' || r.type === 'stop') r.icon = 'fa-subway';
  40. if (r.type === 'attraction') r.icon = 'fa-monument';
  41. if (r.type === 'city' || r.type === 'village' || r.type === 'administrative') r.icon = 'fa-city';
  42. if (r.type === 'administrative') r.icon = 'fa-building';
  43. if (r.type === 'pub' || r.type === 'bar') r.icon = 'fa-beer';
  44. if (r.type === 'hotel' || r.type === 'hostel') r.icon = 'fa-bed';
  45. // ensure we have some city if only town is mentioned
  46. if (!r.address.city) r.address.city = r.address.town;
  47. // name
  48. r.name = r.display_name.split(',')[0];
  49. if (r.class === 'place' || r.class === 'building') r.name = r.address.road + ' ' + r.address.house_number;
  50. });
  51. return results;
  52. }
  53. function removeDuplicates(results) {
  54. var tmp = [];
  55. results.forEach(function (a) {
  56. if (tmp.find(function (b) { return b.place_id === a.place_id; })) return;
  57. tmp.push(a);
  58. });
  59. return tmp;
  60. }
  61. function query(detail, callback) {
  62. var params = {
  63. format: 'json',
  64. limit: 20,
  65. addressdetails: 1,
  66. q: (detail && queryString.indexOf(detail) === -1) ? (queryString + ' , ' + detail) : queryString
  67. };
  68. axios.get('https://nominatim.openstreetmap.org/search.php', {
  69. params: params
  70. }).then(function (response) {
  71. callback(response.data);
  72. }).catch(function (error) {
  73. console.error(error);
  74. });
  75. }
  76. if (!position.details || !position.details.address) return query(null, callback);
  77. var results = [];
  78. query(position.details.address.city + ' , ' + position.details.address.country, function (result) {
  79. results = removeDuplicates(results.concat(result));
  80. if (results.length >= 20) return callback(amendDetails(results));
  81. query(position.details.address.country, function (result) {
  82. results = removeDuplicates(results.concat(result));
  83. if (results.length >= 20) return callback(amendDetails(results));
  84. query(null, function (result) {
  85. results = removeDuplicates(results.concat(result));
  86. return callback(amendDetails(results));
  87. });
  88. });
  89. });
  90. }
  91. app = new Vue({
  92. el: '#app',
  93. data: {
  94. sidePanelOpen: false,
  95. directionsOpen: false,
  96. position: null,
  97. querySearchString: '',
  98. querySearchStringTo: '',
  99. querySearchResults: [],
  100. searchPositionMarker: null,
  101. markerDetailsOpen: false,
  102. markerDetails: {}
  103. },
  104. methods: {
  105. getPositionDetails: function () {
  106. var that = this;
  107. navigator.geolocation.getCurrentPosition(function (position) {
  108. axios.get('https://nominatim.openstreetmap.org/reverse?lat=52&lon=13', {
  109. params: {
  110. format: 'json',
  111. lat: position.coords.latitude,
  112. lon: position.coords.longitude,
  113. }
  114. }).then(function (response) {
  115. that.position = {
  116. coords: position.coords,
  117. details: response.data
  118. };
  119. }).catch(function (error) {
  120. console.error('Unable to get location details:', error);
  121. that.position = {
  122. coords: position.coords,
  123. details: null
  124. };
  125. });
  126. });
  127. },
  128. searchMarkerClicked: function (item) {
  129. this.markerDetailsOpen = true;
  130. this.markerDetails = item;
  131. },
  132. setSearchPositionMarker: function (item) {
  133. var that = this;
  134. if (this.searchPositionMarker) this.searchPositionMarker.remove();
  135. var el = document.createElement('div');
  136. el.addEventListener('click', function (e) {
  137. console.log('marker clicked');
  138. that.searchMarkerClicked(item);
  139. });
  140. el.className = 'search-result-marker';
  141. this.searchPositionMarker = new mapboxgl.Marker(el);
  142. this.searchPositionMarker.setLngLat([ item.lon, item.lat ]);
  143. this.searchPositionMarker.addTo(map);
  144. map.flyTo({ center: [ item.lon, item.lat ], zoom: 16 });
  145. },
  146. searchSelectItem: function (item) {
  147. this.querySearchString = item.display_name;
  148. this.setSearchPositionMarker(item);
  149. },
  150. doQuerySearch: function () {
  151. var that = this;
  152. this.querySearchResults = [];
  153. this.markerDetailsOpen = false;
  154. findResults(this.querySearchString, this.position, function (results) {
  155. that.querySearchResults = results;
  156. that.sidePanelOpen = true;
  157. });
  158. },
  159. searchFocus: function () {
  160. }
  161. },
  162. created: function () {
  163. setupMap();
  164. this.getPositionDetails();
  165. setTimeout(function () {
  166. document.getElementById('querySearchInput').focus();
  167. }, 1000);
  168. }
  169. });