Explorar el Código

Add pubs and current location marker

Johannes Zellner hace 7 años
padre
commit
9caa9a5b79
Se han modificado 2 ficheros con 112 adiciones y 1 borrados
  1. 36 0
      public/index.css
  2. 76 1
      public/index.js

+ 36 - 0
public/index.css

@@ -160,6 +160,17 @@ html, body {
     cursor: pointer;
 }
 
+.poi-icon {
+    color: #8a7862;
+    font-size: 20px;
+    transition: all 0.25s cubic-bezier(.65,.05,.36,1);
+}
+
+.poi-icon:hover {
+    color: #ff0029a6;
+    transform: scale(1.5);
+}
+
 /* element ui patches */
 
 .el-carousel__item {
@@ -172,3 +183,28 @@ html, body {
     width: 128px;
     height: 128px;
 }
+
+@keyframes pulse_animation {
+    0%   { opacity: 0; transform: scale(0); }
+    5%   { opacity: 0.9; transform: scale(1); }
+    80%  { opacity: 0.9; transform: scale(1); }
+    100% { opacity: 0; transform: scale(10); }
+}
+
+.current-location-icon {
+    position: absolute;
+    width: 10px;
+    height: 10px;
+    background-color: #0087ff;
+    border-radius: 5px;
+    animation-name: pulse_animation;
+    animation-duration: 4000ms;
+    transform-origin: 50% 50%;
+    animation-iteration-count: infinite;
+    animation-timing-function: linear;
+}
+
+.current-location-icon-container {
+    border: none;
+    background-color: none;
+}

+ 76 - 1
public/index.js

@@ -1,4 +1,4 @@
-var app, map;
+var app, map, placesMarkerLayer, currentPositionMarker;
 
 var DEFAULT_ZOOM = 15;
 var DEFAULT_MARKER_ICON = L.icon({
@@ -6,6 +6,14 @@ var DEFAULT_MARKER_ICON = L.icon({
     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);
@@ -19,6 +27,8 @@ function setupMap() {
         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');
@@ -29,6 +39,9 @@ function setupMap() {
         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);
         });
     }
@@ -41,6 +54,13 @@ function setupMap() {
         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';
@@ -72,6 +92,17 @@ function setupMap() {
         });
     });
 
+    // 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({
@@ -81,12 +112,56 @@ app = new Vue({
         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;