index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. var app, map, placesMarkerLayer, currentPositionMarker;
  2. var DEFAULT_ZOOM = 15;
  3. var DEFAULT_MARKER_ICON = L.icon({
  4. iconUrl: 'pin.svg',
  5. iconSize: [80, 80]
  6. });
  7. var CURRENT_POSITION_ICON = new L.DivIcon({
  8. className: 'current-location-icon-container',
  9. iconSize: [10, 10],
  10. tooltipAnchor: [0, 50],
  11. html: '<div class="current-location-icon"></div>'
  12. });
  13. function setupMap() {
  14. map = new L.map('map', { zoomControl: false, attributionControl: false });
  15. map.setView([ 52.520008, 13.404954 ], 15);
  16. L.control.scale({ position: 'bottomright', imperial: false }).addTo(map);
  17. // L.control.zoom({ position: 'topright' }).addTo(map);
  18. L.tileLayer('https://api.mapbox.com/styles/v1/nebulon/{id}/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoibmVidWxvbiIsImEiOiI3NmNkODc5ZDMyNjcxMWM2M2UyODEyMjdkM2YyMjVlOSJ9.Cpz0-WRwBwE-DNdNfGDu_w', {
  19. attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
  20. maxZoom: 20,
  21. id: 'cjpmm73gq0fbg2speu42o2y8a',
  22. accessToken: 'pk.eyJ1IjoibmVidWxvbiIsImEiOiI3NmNkODc5ZDMyNjcxMWM2M2UyODEyMjdkM2YyMjVlOSJ9.Cpz0-WRwBwE-DNdNfGDu_w'
  23. }).addTo(map);
  24. placesMarkerLayer = new L.FeatureGroup();
  25. // set map position either based on url or default
  26. var u = new URL(window.location);
  27. var lon = u.searchParams.get('lon');
  28. var lat = u.searchParams.get('lat');
  29. var zoom = u.searchParams.get('zoom');
  30. if (lon && lat) {
  31. map.flyTo(L.latLng(lat, lon), zoom || DEFAULT_ZOOM);
  32. } else {
  33. navigator.geolocation.getCurrentPosition(function (pos) {
  34. currentPositionMarker = L.marker(L.latLng(pos.coords.latitude, pos.coords.longitude), { icon: CURRENT_POSITION_ICON });
  35. currentPositionMarker.addTo(map);
  36. map.flyTo(L.latLng(pos.coords.latitude, pos.coords.longitude), DEFAULT_ZOOM);
  37. });
  38. }
  39. // Update the url bar to match position
  40. map.on('moveend', function (event) {
  41. var u = new URL(window.location);
  42. u.searchParams.set('lon', map.getCenter().lng);
  43. u.searchParams.set('lat', map.getCenter().lat);
  44. u.searchParams.set('zoom', map.getZoom());
  45. history.replaceState({}, window.document.title, u);
  46. app.refreshPlaces();
  47. });
  48. map.on('zoomend', function () {
  49. if (map.getZoom() < 17) map.removeLayer(placesMarkerLayer);
  50. else map.addLayer(placesMarkerLayer);
  51. });
  52. var mapillaryKey = 'SmRiZXFSN3RJYVpKdGd2V3BQZ0dMdzoxZjY3YjA3MmU2OWU2ODll';
  53. map.on('contextmenu ', function (event) {
  54. console.log(event);
  55. var popup = L.popup({ closeButton: false })
  56. .setLatLng(event.latlng)
  57. .setContent('<p>Loading address</p>')
  58. .openOn(map);
  59. var params = {
  60. client_id: mapillaryKey,
  61. closeto: event.latlng.lng + ',' + event.latlng.lat,
  62. pano: false
  63. };
  64. axios.get('https://a.mapillary.com/v3/images', {
  65. params: params
  66. }).then(function (response) {
  67. // console.log(response.data);
  68. if (response.data.features.length === 0) {
  69. popup.setContent('<p>Nothing found</p>');
  70. } else {
  71. popup.setContent('<div class="whats-here" style="background-image: url(https://d1cuyjsrcm0gby.cloudfront.net/' + response.data.features[0].properties.key + '/thumb-640.jpg"></div>');
  72. }
  73. }).catch(function (error) {
  74. console.error(error);
  75. });
  76. });
  77. // watch for position updates
  78. navigator.geolocation.watchPosition(function (pos) {
  79. if (!currentPositionMarker) {
  80. currentPositionMarker = L.marker(L.latLng(pos.coords.latitude, pos.coords.longitude), { icon: CURRENT_POSITION_ICON });
  81. currentPositionMarker.addTo(map);
  82. }
  83. currentPositionMarker.setLatLng(L.latLng(pos.coords.latitude, pos.coords.longitude));
  84. }, function (error) {
  85. console.error('Error watching position:', error);
  86. });
  87. }
  88. app = new Vue({
  89. el: '#app',
  90. data: {
  91. sidePanelOpen: false,
  92. querySearchString: '',
  93. querySearchResults: [],
  94. searchMarker: {},
  95. placesMarker: {},
  96. markerDetailsOpen: false,
  97. markerDetails: {
  98. businessDetails: null
  99. }
  100. },
  101. methods: {
  102. refreshPlaces: function () {
  103. var that = this;
  104. // only refresh places on low zoom
  105. if (map.getZoom() < 17) return;
  106. var params = {
  107. format: 'json',
  108. limit: 100,
  109. addressdetails: 1,
  110. bounded: 1,
  111. viewbox: map.getBounds().toBBoxString(),
  112. q: '[pub]'
  113. };
  114. axios.get('https://nominatim.openstreetmap.org/search.php', {
  115. params: params
  116. }).then(function (response) {
  117. response.data.forEach(function (item) {
  118. if (that.placesMarker[item.place_id]) return;
  119. var m = L.marker(L.latLng(item.lat, item.lon), {
  120. icon: new L.DivIcon({
  121. className: 'my-div-icon',
  122. iconSize: [20, 20],
  123. tooltipAnchor: [0, 50],
  124. html: '<i class="fa fa-glass-martini poi-icon"></i>'
  125. })
  126. });
  127. m.bindTooltip(item.display_name, { direction: 'bottom', offset: [0, 20] });
  128. m.on('click', function (e) {
  129. // that.openPlaceDetails(item);
  130. // that.activateSearchMarker(item);
  131. });
  132. placesMarkerLayer.addLayer(m);
  133. that.placesMarker[item.place_id] = m;
  134. });
  135. }).catch(function (error) {
  136. console.error(error);
  137. });
  138. },
  139. openPlaceDetails: function (item) {
  140. var that = this;
  141. var params = {
  142. name: item.name,
  143. lat: item.lat,
  144. lon: item.lon,
  145. class: item.class
  146. };
  147. axios.get('/api/places/details', {
  148. params: params
  149. }).then(function (response) {
  150. that.sidePanelOpen = true;
  151. that.markerDetailsOpen = true;
  152. that.markerDetails = item;
  153. that.markerDetails.businessDetails = response.data;
  154. }).catch(function (error) {
  155. console.error(error);
  156. that.markerDetailsOpen = true;
  157. that.markerDetails = item;
  158. });
  159. },
  160. addSearchMarker: function (items) {
  161. var that = this;
  162. that.clearSearchMarker();
  163. items.forEach(function (item) {
  164. var m = L.marker(L.latLng(item.lat, item.lon), {
  165. icon: DEFAULT_MARKER_ICON,
  166. opacity: 0.5
  167. });
  168. m.bindTooltip(item.name, { direction: 'bottom' });
  169. m.on('click', function (e) {
  170. that.openPlaceDetails(item);
  171. that.activateSearchMarker(item);
  172. });
  173. m.addTo(map);
  174. that.searchMarker[item.id] = m;
  175. });
  176. },
  177. clearSearchMarker: function () {
  178. var that = this;
  179. Object.keys(this.searchMarker).forEach(function (placeId) {
  180. that.searchMarker[placeId].remove();
  181. });
  182. this.searchMarker = {};
  183. },
  184. activateSearchMarker: function (item) {
  185. var that = this;
  186. Object.keys(this.searchMarker).forEach(function (id) {
  187. that.searchMarker[id].setOpacity(id === item.id ? 1 : 0.6);
  188. });
  189. },
  190. activateSearchItem: function (item) {
  191. map.flyTo(L.latLng(item.lat, item.lon), DEFAULT_ZOOM);
  192. this.activateSearchMarker(item);
  193. },
  194. searchPlaces: function () {
  195. var that = this;
  196. this.querySearchResults = [];
  197. this.markerDetailsOpen = false;
  198. var params = {
  199. search: this.querySearchString,
  200. lat: map.getCenter().lat,
  201. lon: map.getCenter().lng
  202. };
  203. axios.get('/api/places', {
  204. params: params
  205. }).then(function (response) {
  206. that.querySearchResults = response.data;
  207. that.sidePanelOpen = true;
  208. that.addSearchMarker(that.querySearchResults);
  209. if (that.querySearchResults[0]) that.activateSearchItem(that.querySearchResults[0]);
  210. }).catch(function (error) {
  211. console.error(error);
  212. });
  213. }
  214. },
  215. created: function () {
  216. var that = this;
  217. Vue.nextTick(function () {
  218. setupMap();
  219. document.getElementById('querySearchInput').focus();
  220. });
  221. }
  222. });