| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- var app, map, placesMarkerLayer, currentPositionMarker;
- var DEFAULT_ZOOM = 15;
- var DEFAULT_MARKER_ICON = L.icon({
- iconUrl: 'pin.svg',
- iconSize: [80, 80]
- });
- var CURRENT_POSITION_ICON = new L.DivIcon({
- className: 'current-location-icon-container',
- iconSize: [10, 10],
- tooltipAnchor: [0, 50],
- html: '<div class="current-location-icon"></div>'
- });
- function setupMap() {
- map = new L.map('map', { zoomControl: false, attributionControl: false });
- map.setView([ 52.520008, 13.404954 ], 15);
- 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 © <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>',
- maxZoom: 20,
- id: 'cjpmm73gq0fbg2speu42o2y8a',
- accessToken: 'pk.eyJ1IjoibmVidWxvbiIsImEiOiI3NmNkODc5ZDMyNjcxMWM2M2UyODEyMjdkM2YyMjVlOSJ9.Cpz0-WRwBwE-DNdNfGDu_w'
- }).addTo(map);
- placesMarkerLayer = new L.FeatureGroup();
- // 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) {
- currentPositionMarker = L.marker(L.latLng(pos.coords.latitude, pos.coords.longitude), { icon: CURRENT_POSITION_ICON });
- currentPositionMarker.addTo(map);
- 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);
- app.refreshPlaces();
- });
- map.on('zoomend', function () {
- if (map.getZoom() < 17) map.removeLayer(placesMarkerLayer);
- else map.addLayer(placesMarkerLayer);
- });
- var mapillaryKey = 'SmRiZXFSN3RJYVpKdGd2V3BQZ0dMdzoxZjY3YjA3MmU2OWU2ODll';
- map.on('contextmenu ', function (event) {
- console.log(event);
- var popup = L.popup({ closeButton: false })
- .setLatLng(event.latlng)
- .setContent('<p>Loading address</p>')
- .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('<p>Nothing found</p>');
- } else {
- popup.setContent('<div class="whats-here" style="background-image: url(https://d1cuyjsrcm0gby.cloudfront.net/' + response.data.features[0].properties.key + '/thumb-640.jpg"></div>');
- }
- }).catch(function (error) {
- console.error(error);
- });
- });
- // watch for position updates
- navigator.geolocation.watchPosition(function (pos) {
- if (!currentPositionMarker) {
- currentPositionMarker = L.marker(L.latLng(pos.coords.latitude, pos.coords.longitude), { icon: CURRENT_POSITION_ICON });
- currentPositionMarker.addTo(map);
- }
- currentPositionMarker.setLatLng(L.latLng(pos.coords.latitude, pos.coords.longitude));
- }, function (error) {
- console.error('Error watching position:', error);
- });
- }
- app = new Vue({
- el: '#app',
- data: {
- sidePanelOpen: false,
- querySearchString: '',
- querySearchResults: [],
- searchMarker: {},
- placesMarker: {},
- markerDetailsOpen: false,
- markerDetails: {
- businessDetails: null
- }
- },
- methods: {
- refreshPlaces: function () {
- var that = this;
- // only refresh places on low zoom
- if (map.getZoom() < 17) return;
- var params = {
- format: 'json',
- limit: 100,
- addressdetails: 1,
- bounded: 1,
- viewbox: map.getBounds().toBBoxString(),
- q: '[pub]'
- };
- axios.get('https://nominatim.openstreetmap.org/search.php', {
- params: params
- }).then(function (response) {
- response.data.forEach(function (item) {
- if (that.placesMarker[item.place_id]) return;
- var m = L.marker(L.latLng(item.lat, item.lon), {
- icon: new L.DivIcon({
- className: 'my-div-icon',
- iconSize: [20, 20],
- tooltipAnchor: [0, 50],
- html: '<i class="fa fa-glass-martini poi-icon"></i>'
- })
- });
- m.bindTooltip(item.display_name, { direction: 'bottom', offset: [0, 20] });
- m.on('click', function (e) {
- // that.openPlaceDetails(item);
- // that.activateSearchMarker(item);
- });
- placesMarkerLayer.addLayer(m);
- that.placesMarker[item.place_id] = m;
- });
- }).catch(function (error) {
- console.error(error);
- });
- },
- 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();
- });
- }
- });
|