| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- mapboxgl.accessToken = 'pk.eyJ1IjoibmVidWxvbiIsImEiOiI3NmNkODc5ZDMyNjcxMWM2M2UyODEyMjdkM2YyMjVlOSJ9.Cpz0-WRwBwE-DNdNfGDu_w';
- var app, map;
- function setupMap() {
- map = new mapboxgl.Map({
- container: 'map',
- // style: 'mapbox://styles/nebulon/cjpa4rr8f0bjw2sruosd5r196',
- style: 'mapbox://styles/nebulon/cjpmm73gq0fbg2speu42o2y8a', // with railway stations
- center: [ 13.404954, 52.520008 ], // Berlin
- zoom: 5
- });
- 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');
- // 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.jumpTo({ center: [ lon, lat ], zoom: zoom || 12 });
- } else {
- navigator.geolocation.getCurrentPosition(function (position) {
- map.jumpTo({ center: [ position.coords.longitude, position.coords.latitude ], zoom: 12 });
- });
- }
- // 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 = new Vue({
- el: '#app',
- data: {
- sidePanelOpen: false,
- directionsOpen: false,
- querySearchString: '',
- querySearchStringTo: '',
- querySearchResults: [],
- searchPositionMarker: null,
- 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.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 el = document.createElement('div');
- el.className = 'search-result-marker';
- el.addEventListener('click', function (e) {
- that.openPlaceDetails(item);
- });
- var m = new mapboxgl.Marker({ element: el });
- m.setLngLat([ item.lon, item.lat ]);
- m.addTo(map);
- that.searchMarker.push(m);
- });
- },
- clearSearchMarker: function () {
- this.searchMarker.forEach(function (marker) {
- marker.remove();
- });
- this.searchMarker = [];
- },
- activateSearchItem: function (item) {
- this.querySearchString = item.display_name;
- map.flyTo({ center: [ item.lon, item.lat ], zoom: 16 });
- },
- 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.slice(0,1));
- if (that.querySearchResults[0]) map.flyTo({ center: [ that.querySearchResults[0].lon, that.querySearchResults[0].lat ], zoom: 16 });
- }).catch(function (error) {
- console.error(error);
- });
- }
- },
- created: function () {
- var that = this;
- Vue.nextTick(function () {
- setupMap();
- document.getElementById('querySearchInput').focus();
- });
- }
- });
|