mapboxgl.accessToken = 'pk.eyJ1IjoibmVidWxvbiIsImEiOiI3NmNkODc5ZDMyNjcxMWM2M2UyODEyMjdkM2YyMjVlOSJ9.Cpz0-WRwBwE-DNdNfGDu_w'; var app, map; function setupMap() { navigator.geolocation.getCurrentPosition(function (position) { map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/nebulon/cjpa4rr8f0bjw2sruosd5r196', center: [ position.coords.longitude, position.coords.latitude ], zoom: 12.0 }); var scale = new mapboxgl.ScaleControl({ maxWidth: 80, unit: 'metric' }); map.addControl(scale, 'bottom-right'); const locationControl = new mapboxgl.GeolocateControl({ positionOptions: { enableHighAccuracy: true }, trackUserLocation: true }); map.addControl(locationControl); var nav = new mapboxgl.NavigationControl(); map.addControl(nav, 'top-right'); }); } function findResults(queryString, position, callback) { function amendDetails(results) { results.forEach(function (r) { // icons r.icon = 'fa-address-card'; if (r.type === 'restaurant') r.icon = 'fa-utensils'; if (r.type === 'doctors') r.icon = 'fa-user-md'; if (r.type === 'dentist') r.icon = 'fa-tooth'; if (r.type === 'car') r.icon = 'fa-car'; if (r.type === 'bicycle-rental') r.icon = 'fa-bicycle'; if (r.type === 'school') r.icon = 'fa-graduation-cap'; if (r.type === 'canal' || r.type === 'river' || r.type === 'water') r.icon = 'fa-water'; if (r.type === 'station' || r.type === 'stop') r.icon = 'fa-subway'; if (r.type === 'attraction') r.icon = 'fa-monument'; if (r.type === 'city' || r.type === 'village' || r.type === 'administrative') r.icon = 'fa-city'; if (r.type === 'administrative') r.icon = 'fa-building'; if (r.type === 'pub' || r.type === 'bar') r.icon = 'fa-beer'; if (r.type === 'hotel' || r.type === 'hostel') r.icon = 'fa-bed'; // ensure we have some city if only town is mentioned if (!r.address.city) r.address.city = r.address.town; // name r.name = r.display_name.split(',')[0]; if (r.class === 'place' || r.class === 'building') r.name = r.address.road + ' ' + r.address.house_number; }); return results; } function removeDuplicates(results) { var tmp = []; results.forEach(function (a) { if (tmp.find(function (b) { return b.place_id === a.place_id; })) return; tmp.push(a); }); return tmp; } function query(detail, callback) { var params = { format: 'json', limit: 20, addressdetails: 1, q: (detail && queryString.indexOf(detail) === -1) ? (queryString + ' , ' + detail) : queryString }; axios.get('https://nominatim.openstreetmap.org/search.php', { params: params }).then(function (response) { callback(response.data); }).catch(function (error) { console.error(error); }); } if (!position.details || !position.details.address) return query(null, callback); var results = []; query(position.details.address.city + ' , ' + position.details.address.country, function (result) { results = removeDuplicates(results.concat(result)); if (results.length >= 20) return callback(amendDetails(results)); query(position.details.address.country, function (result) { results = removeDuplicates(results.concat(result)); if (results.length >= 20) return callback(amendDetails(results)); query(null, function (result) { results = removeDuplicates(results.concat(result)); return callback(amendDetails(results)); }); }); }); } app = new Vue({ el: '#app', data: { sidePanelOpen: false, directionsOpen: false, position: null, querySearchString: '', querySearchStringTo: '', querySearchResults: [], searchPositionMarker: null, markerDetailsOpen: false, markerDetails: {} }, methods: { getPositionDetails: function () { var that = this; navigator.geolocation.getCurrentPosition(function (position) { axios.get('https://nominatim.openstreetmap.org/reverse?lat=52&lon=13', { params: { format: 'json', lat: position.coords.latitude, lon: position.coords.longitude, } }).then(function (response) { that.position = { coords: position.coords, details: response.data }; }).catch(function (error) { console.error('Unable to get location details:', error); that.position = { coords: position.coords, details: null }; }); }); }, searchMarkerClicked: function (item) { this.markerDetailsOpen = true; this.markerDetails = item; }, setSearchPositionMarker: function (item) { var that = this; if (this.searchPositionMarker) this.searchPositionMarker.remove(); var el = document.createElement('div'); el.addEventListener('click', function (e) { console.log('marker clicked'); that.searchMarkerClicked(item); }); el.className = 'search-result-marker'; this.searchPositionMarker = new mapboxgl.Marker(el); this.searchPositionMarker.setLngLat([ item.lon, item.lat ]); this.searchPositionMarker.addTo(map); map.flyTo({ center: [ item.lon, item.lat ], zoom: 16 }); }, searchSelectItem: function (item) { this.querySearchString = item.display_name; this.setSearchPositionMarker(item); }, doQuerySearch: function () { var that = this; this.querySearchResults = []; this.markerDetailsOpen = false; findResults(this.querySearchString, this.position, function (results) { that.querySearchResults = results; that.sidePanelOpen = true; }); }, searchFocus: function () { } }, created: function () { setupMap(); this.getPositionDetails(); setTimeout(function () { document.getElementById('querySearchInput').focus(); }, 1000); } });