index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 removeDuplicates(results) {
  29. var tmp = [];
  30. results.forEach(function (a) {
  31. if (tmp.find(function (b) { return b.place_id === a.place_id; })) return;
  32. tmp.push(a);
  33. });
  34. return tmp;
  35. }
  36. function query(detail, callback) {
  37. var params = {
  38. format: 'json',
  39. limit: 20,
  40. q: (detail && queryString.indexOf(detail) === -1) ? (queryString + ' , ' + detail) : queryString
  41. };
  42. axios.get('https://nominatim.openstreetmap.org/search.php', {
  43. params: params
  44. }).then(function (response) {
  45. callback(response.data);
  46. }).catch(function (error) {
  47. console.error(error);
  48. });
  49. }
  50. if (!position.details || !position.details.address) return query(null, callback);
  51. var results = [];
  52. query(position.details.address.city + ' , ' + position.details.address.country, function (result) {
  53. results = removeDuplicates(results.concat(result));
  54. if (results.length >= 20) return callback(results);
  55. query(position.details.address.country, function (result) {
  56. results = removeDuplicates(results.concat(result));
  57. if (results.length >= 20) return callback(results);
  58. query(null, function (result) {
  59. results = removeDuplicates(results.concat(result));
  60. return callback(results);
  61. });
  62. });
  63. });
  64. }
  65. app = new Vue({
  66. el: '#app',
  67. data: {
  68. sidePanelOpen: false,
  69. directionsOpen: false,
  70. position: null,
  71. querySearchString: '',
  72. querySearchStringTo: '',
  73. querySearchResults: [],
  74. searchPositionMarker: null,
  75. markerDetailsOpen: false,
  76. markerDetails: {}
  77. },
  78. methods: {
  79. getPositionDetails: function () {
  80. var that = this;
  81. navigator.geolocation.getCurrentPosition(function (position) {
  82. axios.get('https://nominatim.openstreetmap.org/reverse?lat=52&lon=13', {
  83. params: {
  84. format: 'json',
  85. lat: position.coords.latitude,
  86. lon: position.coords.longitude,
  87. }
  88. }).then(function (response) {
  89. that.position = {
  90. coords: position.coords,
  91. details: response.data
  92. };
  93. }).catch(function (error) {
  94. console.error('Unable to get location details:', error);
  95. that.position = {
  96. coords: position.coords,
  97. details: null
  98. };
  99. });
  100. });
  101. },
  102. searchMarkerClicked: function (item) {
  103. this.markerDetailsOpen = true;
  104. this.markerDetails = item;
  105. },
  106. setSearchPositionMarker: function (item) {
  107. var that = this;
  108. if (this.searchPositionMarker) this.searchPositionMarker.remove();
  109. var el = document.createElement('div');
  110. el.addEventListener('click', function (e) {
  111. console.log('marker clicked');
  112. that.searchMarkerClicked(item);
  113. });
  114. el.className = 'search-result-marker';
  115. this.searchPositionMarker = new mapboxgl.Marker(el);
  116. this.searchPositionMarker.setLngLat([ item.lon, item.lat ]);
  117. this.searchPositionMarker.addTo(map);
  118. map.flyTo({ center: [ item.lon, item.lat ], zoom: 16 });
  119. },
  120. searchSelectItem: function (item) {
  121. this.querySearchString = item.display_name;
  122. this.setSearchPositionMarker(item);
  123. },
  124. doQuerySearch: function () {
  125. var that = this;
  126. this.querySearchResults = [];
  127. this.markerDetailsOpen = false;
  128. findResults(this.querySearchString, this.position, function (results) {
  129. that.querySearchResults = results;
  130. that.sidePanelOpen = true;
  131. });
  132. },
  133. searchFocus: function () {
  134. }
  135. },
  136. created: function () {
  137. setupMap();
  138. this.getPositionDetails();
  139. }
  140. });