index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. position: null,
  61. search: '',
  62. searchPositionMarker: null
  63. },
  64. methods: {
  65. getPositionDetails: function () {
  66. var that = this;
  67. navigator.geolocation.getCurrentPosition(function (position) {
  68. axios.get('https://nominatim.openstreetmap.org/reverse?lat=52&lon=13', {
  69. params: {
  70. format: 'json',
  71. lat: position.coords.latitude,
  72. lon: position.coords.longitude,
  73. }
  74. }).then(function (response) {
  75. that.position = {
  76. coords: position.coords,
  77. details: response.data
  78. };
  79. }).catch(function (error) {
  80. console.error('Unable to get location details:', error);
  81. that.position = {
  82. coords: position.coords,
  83. details: null
  84. };
  85. });
  86. });
  87. },
  88. setSearchPositionMarker: function (item) {
  89. if (this.searchPositionMarker) this.searchPositionMarker.remove();
  90. this.searchPositionMarker = new mapboxgl.Marker();
  91. this.searchPositionMarker.setLngLat([ item.lon, item.lat ]);
  92. this.searchPositionMarker.addTo(map);
  93. map.flyTo({ center: [ item.lon, item.lat ], zoom: 16 });
  94. },
  95. searchSelectItem: function (item) {
  96. this.search = item.display_name;
  97. this.setSearchPositionMarker(item);
  98. },
  99. searchNow: function (queryString, callback) {
  100. findResults(queryString, this.position, callback);
  101. },
  102. searchFocus: function () {
  103. }
  104. },
  105. created: function () {
  106. setupMap();
  107. this.getPositionDetails();
  108. }
  109. });