From 9ec7e4be14f908cb190c0d935477d7e78a0e549b Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Tue, 10 Sep 2019 16:59:59 +0200 Subject: [PATCH] add locate me button; fix zoom level --- app/views/location.ejs | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/app/views/location.ejs b/app/views/location.ejs index 57a339a..acff901 100644 --- a/app/views/location.ejs +++ b/app/views/location.ejs @@ -1,12 +1,15 @@ <% include partials/navBar %>
-
-
- -
+
+
-
+
+ gps_fixed +
+
+
+
@@ -25,6 +28,27 @@ let autocomplete; let map; let places; + let geocoder; + + function locateMe() { + if (navigator.geolocation) { + + const onLocationSuccess = (position) => { + const coordinates = position && position.coords ? position.coords : null; + if (coordinates){ + const longitude = coordinates.longitude || null; + const latitude = coordinates.latitude || null; + + if (longitude && latitude && map){ + map.setCenter({lat: latitude, lng: longitude}); + map.setZoom(16); + } + } + }; + + navigator.geolocation.getCurrentPosition(onLocationSuccess); + } + } function initMap() { const BOSNIA_BOUNDS = { @@ -70,8 +94,7 @@ function onPlaceChanged() { const place = autocomplete.getPlace(); if (place.geometry) { - map.panTo(place.geometry.location); - map.setZoom(15); + map.fitBounds(place.geometry.viewport); $("#locationInputData").val(JSON.stringify(place)); } } @@ -89,6 +112,7 @@ $("#form-map-output").submit(); }); + document.getElementById("locateMe").addEventListener("click", locateMe); });