index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. var overpassFrontend = new OverpassFrontend('//overpass-api.de/api/interpreter', {
  14. timeGap: 10,
  15. effortPerRequest: 100
  16. });
  17. function setupMap() {
  18. map = new L.map('map', { zoomControl: false, attributionControl: false });
  19. map.setView([ 52.520008, 13.404954 ], 15);
  20. L.control.scale({ position: 'bottomright', imperial: false }).addTo(map);
  21. // L.control.zoom({ position: 'topright' }).addTo(map);
  22. mapboxLayer = L.tileLayer('https://api.mapbox.com/styles/v1/nebulon/{id}/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoibmVidWxvbiIsImEiOiI3NmNkODc5ZDMyNjcxMWM2M2UyODEyMjdkM2YyMjVlOSJ9.Cpz0-WRwBwE-DNdNfGDu_w', {
  23. 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>',
  24. maxZoom: 20,
  25. id: 'cjpmm73gq0fbg2speu42o2y8a',
  26. accessToken: 'pk.eyJ1IjoibmVidWxvbiIsImEiOiI3NmNkODc5ZDMyNjcxMWM2M2UyODEyMjdkM2YyMjVlOSJ9.Cpz0-WRwBwE-DNdNfGDu_w'
  27. });
  28. satelliteLayer = L.tileLayer('http://{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}',{
  29. maxZoom: 20,
  30. subdomains:['mt0','mt1','mt2','mt3']
  31. });
  32. var baseLayers = {
  33. 'Map': mapboxLayer,
  34. 'Satellite': satelliteLayer
  35. };
  36. L.control.layers(baseLayers, {}).addTo(map);
  37. // set the default layers
  38. map.addLayer(mapboxLayer);
  39. var amenityTypes = 'restaurant|cafe|bar|pub|pharmacy|toilets';
  40. var amenityLayer = new OverpassLayer({
  41. query: '(node[amenity~"^(' + amenityTypes + ')$"];way[amenity~"^(' + amenityTypes + ')$"];relation[amenity~"^(' + amenityTypes + ')$"];);',
  42. minZoom: 18,
  43. updateAssets: function (domNode, objectData, layer) {
  44. domNode.onclick = function (event) {
  45. event.stopPropagation();
  46. app.openPoi(objectData.twigData);
  47. };
  48. },
  49. feature: {
  50. body: '',
  51. style: {
  52. width: 0,
  53. fill: false
  54. },
  55. markerSign: '<span>{{ tags.name }}</span>',
  56. markerSymbol: '{% if tags.amenity=="restaurant" %}<i class="fas fa-utensils"></i>' +
  57. '{% elseif tags.amenity=="cafe" %}<i class="fas fa-coffee"></i>' +
  58. '{% elseif tags.amenity=="bar" %}<i class="fas fa-glass-cheers"></i>' +
  59. '{% elseif tags.amenity=="pub" %}<i class="fas fa-beer"></i>' +
  60. '{% elseif tags.amenity=="pharmacy" %}<i class="fas fa-prescription-bottle-alt"></i>' +
  61. '{% elseif tags.amenity=="toilets" %}<i class="fas fa-toilet-paper"></i>' +
  62. '{% endif %}'
  63. }
  64. });
  65. amenityLayer.addTo(map);
  66. var publicTransportLayer = new OverpassLayer({
  67. query: '(node[public_transport="stop_position"];node[public_transport="station"];);',
  68. minZoom: 18,
  69. updateAssets: function (domNode, objectData, layer) {
  70. domNode.onclick = function (event) {
  71. event.stopPropagation();
  72. app.openPoi(objectData.twigData);
  73. };
  74. },
  75. feature: {
  76. body: '',
  77. style: {
  78. width: 0,
  79. fill: false
  80. },
  81. // markerSign: '<span>{{ tags.name }}</span>',
  82. markerSymbol: '{% if tags.subway=="yes" %}<img src="ubahn.svg" width="32" height="32"/>' +
  83. '{% elseif tags.bus=="yes" %}<img src="bus.svg" width="32" height="32"/>' +
  84. '{% elseif tags.light_rail=="yes" %}<img src="sbahn.svg" width="32" height="32"/>' +
  85. '{% elseif tags.tram=="yes" %}<img src="tram.svg" width="32" height="32"/>' +
  86. '{% endif %}'
  87. }
  88. });
  89. publicTransportLayer.addTo(map);
  90. L.easyPrint({
  91. title: 'Export tis map!',
  92. position: 'topright',
  93. sizeModes: ['A4Portrait', 'A4Landscape'],
  94. exportOnly: true
  95. }).addTo(map);
  96. L.control.measure({
  97. position: 'topright',
  98. primaryLengthUnit: 'meters',
  99. primaryAreaUnit: 'sqmeters',
  100. activeColor: '#555555',
  101. completedColor: '#000000'
  102. }).addTo(map);
  103. placesMarkerLayer = new L.FeatureGroup();
  104. // set map position either based on url or default
  105. var u = new URL(window.location);
  106. var lon = u.searchParams.get('lon');
  107. var lat = u.searchParams.get('lat');
  108. var zoom = u.searchParams.get('zoom');
  109. if (lon && lat) {
  110. map.flyTo(L.latLng(lat, lon), zoom || DEFAULT_ZOOM);
  111. } else {
  112. navigator.geolocation.getCurrentPosition(function (pos) {
  113. currentPositionMarker = L.marker(L.latLng(pos.coords.latitude, pos.coords.longitude), { icon: CURRENT_POSITION_ICON });
  114. currentPositionMarker.addTo(map);
  115. map.flyTo(L.latLng(pos.coords.latitude, pos.coords.longitude), DEFAULT_ZOOM);
  116. });
  117. }
  118. // Update the url bar to match position
  119. map.on('moveend', function (event) {
  120. var u = new URL(window.location);
  121. u.searchParams.set('lon', map.getCenter().lng);
  122. u.searchParams.set('lat', map.getCenter().lat);
  123. u.searchParams.set('zoom', map.getZoom());
  124. history.replaceState({}, window.document.title, u);
  125. });
  126. map.on('zoomend', function () {
  127. if (map.getZoom() < 17) map.removeLayer(placesMarkerLayer);
  128. else map.addLayer(placesMarkerLayer);
  129. });
  130. map.on('contextmenu ', function (event) {
  131. var menu = document.getElementById('contextMenu');
  132. function contextMenuDismissHandler() {
  133. menu.classList.remove('visible');
  134. document.removeEventListener('click', contextMenuDismissHandler);
  135. }
  136. document.addEventListener('click', contextMenuDismissHandler);
  137. menu.classList.add('visible');
  138. menu.style.left = event.originalEvent.clientX + 'px';
  139. menu.style.top = event.originalEvent.clientY + 'px';
  140. });
  141. // watch for position updates
  142. navigator.geolocation.watchPosition(function (pos) {
  143. if (!currentPositionMarker) {
  144. currentPositionMarker = L.marker(L.latLng(pos.coords.latitude, pos.coords.longitude), { icon: CURRENT_POSITION_ICON });
  145. currentPositionMarker.addTo(map);
  146. }
  147. currentPositionMarker.setLatLng(L.latLng(pos.coords.latitude, pos.coords.longitude));
  148. }, function (error) {
  149. console.error('Error watching position:', error);
  150. });
  151. }
  152. app = new Vue({
  153. el: '#app',
  154. data: {
  155. sidePanelOpen: false,
  156. sidePanelBusy: false,
  157. querySearchString: '',
  158. querySearchResults: [],
  159. searchMarker: {},
  160. placesMarker: {},
  161. markerDetailsOpen: false,
  162. markerDetails: {
  163. businessDetails: null
  164. },
  165. poiDetails: {}
  166. },
  167. methods: {
  168. openPoi: function (item) {
  169. console.log(item.tags)
  170. this.poiDetails = item;
  171. this.sidePanelOpen = true;
  172. this.markerDetailsOpen = true;
  173. },
  174. openPlaceDetails: function (item) {
  175. var that = this;
  176. var params = {
  177. name: item.display_name,
  178. lat: item.lat,
  179. lon: item.lon,
  180. class: item.class
  181. };
  182. axios.get('/api/places/details', {
  183. params: params
  184. }).then(function (response) {
  185. that.sidePanelOpen = true;
  186. that.markerDetailsOpen = true;
  187. that.markerDetails = item;
  188. that.markerDetails.businessDetails = response.data;
  189. }).catch(function (error) {
  190. console.error(error);
  191. that.markerDetailsOpen = true;
  192. that.markerDetails = item;
  193. });
  194. },
  195. addSearchMarker: function (items) {
  196. var that = this;
  197. that.clearSearchMarker();
  198. items.forEach(function (item) {
  199. var m = L.marker(L.latLng(item.lat, item.lon), {
  200. icon: DEFAULT_MARKER_ICON,
  201. opacity: 0.5
  202. });
  203. m.bindTooltip(item.display_name, { direction: 'bottom' });
  204. m.on('click', function (e) {
  205. that.openPlaceDetails(item);
  206. that.activateSearchMarker(item);
  207. });
  208. m.addTo(map);
  209. that.searchMarker[item.id] = m;
  210. });
  211. },
  212. clearSearchMarker: function () {
  213. var that = this;
  214. Object.keys(this.searchMarker).forEach(function (placeId) {
  215. that.searchMarker[placeId].remove();
  216. });
  217. this.searchMarker = {};
  218. },
  219. activateSearchMarker: function (item) {
  220. var that = this;
  221. Object.keys(this.searchMarker).forEach(function (id) {
  222. that.searchMarker[id].setOpacity(id === item.id ? 1 : 0.6);
  223. });
  224. },
  225. activateSearchItem: function (item) {
  226. map.flyTo(L.latLng(item.lat, item.lon), DEFAULT_ZOOM);
  227. this.activateSearchMarker(item);
  228. },
  229. searchPlaces: function () {
  230. var that = this;
  231. this.querySearchResults = [];
  232. this.markerDetailsOpen = false;
  233. this.sidePanelOpen = true;
  234. this.sidePanelBusy = true;
  235. var params = {
  236. q: this.querySearchString,
  237. addressdetails: 1,
  238. extratags: 1,
  239. format: 'json'
  240. };
  241. axios.get('https://nominatim.openstreetmap.org/search', {
  242. params: params
  243. }).then(function (response) {
  244. console.log(response.data)
  245. that.querySearchResults = response.data;
  246. that.querySearchResults.forEach(function (d) {
  247. // icons
  248. d.icon = 'fa-address-card';
  249. if (d.type === 'fast_food' || d.type === 'restaurant') d.icon = 'fa-utensils';
  250. if (d.type === 'doctors') d.icon = 'fa-user-md';
  251. if (d.type === 'beach') d.icon = 'fa-umbrella-beach';
  252. if (d.type === 'dentist') d.icon = 'fa-tooth';
  253. if (d.type === 'car') d.icon = 'fa-car';
  254. if (d.type === 'playground') d.icon = 'fa-child';
  255. if (d.type === 'recreation_ground') d.icon = 'fa-running';
  256. if (d.type === 'sports_centre' || d.type === 'pitch') d.icon = 'fa-futbol';
  257. if (d.type === 'bicycle-rental') d.icon = 'fa-bicycle';
  258. if (d.type === 'school') d.icon = 'fa-graduation-cap';
  259. if (d.type === 'post_box' || d.type === 'post_office') d.icon = 'fa-envelope-square';
  260. if (d.type === 'canal' || d.type === 'river' || d.type === 'water') d.icon = 'fa-water';
  261. if (d.type === 'station' || d.type === 'stop' || d.type === 'bus_stop') d.icon = 'fa-subway';
  262. if (d.type === 'attraction') d.icon = 'fa-monument';
  263. if (d.type === 'city' || d.type === 'village' || d.type === 'town' || d.type === 'hamlet') d.icon = 'fa-city';
  264. if (d.type === 'administrative' || d.class === 'office') d.icon = 'fa-building';
  265. if (d.type === 'pub' || d.type === 'bar') d.icon = 'fa-beer';
  266. if (d.type === 'nightclub') d.icon = 'fa-glass-cheers';
  267. if (d.type === 'hotel' || d.type === 'hostel') d.icon = 'fa-bed';
  268. if (d.type === 'supermarket' || d.type === 'retail') d.icon = 'fa-shopping-cart';
  269. if (d.class === 'highway') d.icon = 'fa-road';
  270. });
  271. that.sidePanelBusy = false;
  272. that.addSearchMarker(that.querySearchResults);
  273. }).catch(function (error) {
  274. console.error(error);
  275. });
  276. }
  277. // searchPlaces: function () {
  278. // var that = this;
  279. // this.querySearchResults = [];
  280. // this.markerDetailsOpen = false;
  281. // this.sidePanelOpen = true;
  282. // this.sidePanelBusy = true;
  283. // var params = {
  284. // search: this.querySearchString,
  285. // lat: map.getCenter().lat,
  286. // lon: map.getCenter().lng
  287. // };
  288. // axios.get('/api/places', {
  289. // params: params
  290. // }).then(function (response) {
  291. // that.querySearchResults = response.data;
  292. // that.sidePanelBusy = false;
  293. // that.addSearchMarker(that.querySearchResults);
  294. // }).catch(function (error) {
  295. // console.error(error);
  296. // });
  297. // }
  298. },
  299. created: function () {
  300. var that = this;
  301. Vue.nextTick(function () {
  302. setupMap();
  303. document.getElementById('querySearchInput').focus();
  304. });
  305. }
  306. });