index.js 7.3 KB

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