index.js 4.9 KB

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