var app, map; var DEFAULT_ZOOM = 15; var DEFAULT_MARKER_ICON = L.icon({ iconUrl: 'pin.svg', iconSize: [80, 80] }); function setupMap() { map = new L.map('map', { zoomControl: false, attributionControl: false }); map.setView([ 52.520008, 13.404954 ], 20); L.control.scale({ position: 'bottomright', imperial: false }).addTo(map); // L.control.zoom({ position: 'topright' }).addTo(map); L.tileLayer('https://api.mapbox.com/styles/v1/nebulon/{id}/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoibmVidWxvbiIsImEiOiI3NmNkODc5ZDMyNjcxMWM2M2UyODEyMjdkM2YyMjVlOSJ9.Cpz0-WRwBwE-DNdNfGDu_w', { attribution: 'Map data © OpenStreetMap contributors, CC-BY-SA, Imagery © Mapbox', maxZoom: 18, id: 'cjpmm73gq0fbg2speu42o2y8a', accessToken: 'pk.eyJ1IjoibmVidWxvbiIsImEiOiI3NmNkODc5ZDMyNjcxMWM2M2UyODEyMjdkM2YyMjVlOSJ9.Cpz0-WRwBwE-DNdNfGDu_w' }).addTo(map); // set map position either based on url or default var u = new URL(window.location); var lon = u.searchParams.get('lon'); var lat = u.searchParams.get('lat'); var zoom = u.searchParams.get('zoom'); if (lon && lat) { map.flyTo(L.latLng(lat, lon), zoom || DEFAULT_ZOOM); } else { navigator.geolocation.getCurrentPosition(function (pos) { map.flyTo(L.latLng(pos.coords.latitude, pos.coords.longitude), DEFAULT_ZOOM); }); } // Update the url bar to match position map.on('moveend', function (event) { var u = new URL(window.location); u.searchParams.set('lon', map.getCenter().lng); u.searchParams.set('lat', map.getCenter().lat); u.searchParams.set('zoom', map.getZoom()); history.replaceState({}, window.document.title, u); }); var mapillaryKey = 'SmRiZXFSN3RJYVpKdGd2V3BQZ0dMdzoxZjY3YjA3MmU2OWU2ODll'; map.on('contextmenu ', function (event) { console.log(event); var popup = L.popup({ closeButton: false }) .setLatLng(event.latlng) .setContent('
Loading address
') .openOn(map); var params = { client_id: mapillaryKey, closeto: event.latlng.lng + ',' + event.latlng.lat, pano: false }; axios.get('https://a.mapillary.com/v3/images', { params: params }).then(function (response) { // console.log(response.data); if (response.data.features.length === 0) { popup.setContent('Nothing found
'); } else { popup.setContent(''); } }).catch(function (error) { console.error(error); }); }); } app = new Vue({ el: '#app', data: { sidePanelOpen: false, querySearchString: '', querySearchResults: [], searchMarker: {}, markerDetailsOpen: false, markerDetails: { businessDetails: null } }, methods: { openPlaceDetails: function (item) { var that = this; var params = { name: item.name, lat: item.lat, lon: item.lon, class: item.class }; axios.get('/api/places/details', { params: params }).then(function (response) { that.sidePanelOpen = true; that.markerDetailsOpen = true; that.markerDetails = item; that.markerDetails.businessDetails = response.data; }).catch(function (error) { console.error(error); that.markerDetailsOpen = true; that.markerDetails = item; }); }, addSearchMarker: function (items) { var that = this; that.clearSearchMarker(); items.forEach(function (item) { var m = L.marker(L.latLng(item.lat, item.lon), { icon: DEFAULT_MARKER_ICON, opacity: 0.5 }); m.bindTooltip(item.name, { direction: 'bottom' }); m.on('click', function (e) { that.openPlaceDetails(item); that.activateSearchMarker(item); }); m.addTo(map); that.searchMarker[item.id] = m; }); }, clearSearchMarker: function () { var that = this; Object.keys(this.searchMarker).forEach(function (placeId) { that.searchMarker[placeId].remove(); }); this.searchMarker = {}; }, activateSearchMarker: function (item) { var that = this; Object.keys(this.searchMarker).forEach(function (id) { that.searchMarker[id].setOpacity(id === item.id ? 1 : 0.6); }); }, activateSearchItem: function (item) { map.flyTo(L.latLng(item.lat, item.lon), DEFAULT_ZOOM); this.activateSearchMarker(item); }, searchPlaces: function () { var that = this; this.querySearchResults = []; this.markerDetailsOpen = false; var params = { search: this.querySearchString, lat: map.getCenter().lat, lon: map.getCenter().lng }; axios.get('/api/places', { params: params }).then(function (response) { that.querySearchResults = response.data; that.sidePanelOpen = true; that.addSearchMarker(that.querySearchResults); if (that.querySearchResults[0]) that.activateSearchItem(that.querySearchResults[0]); }).catch(function (error) { console.error(error); }); } }, created: function () { var that = this; Vue.nextTick(function () { setupMap(); document.getElementById('querySearchInput').focus(); }); } });