From 7e01233e5252ff8cf3bb2ec988c7a11048c088fc Mon Sep 17 00:00:00 2001 From: Kimmo Puputti Date: Mon, 13 Aug 2018 10:49:01 +0300 Subject: [PATCH] Add helper to get user's location --- src/util/maps.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/util/maps.js b/src/util/maps.js index 94d95ea3..d7ca2219 100644 --- a/src/util/maps.js +++ b/src/util/maps.js @@ -64,3 +64,25 @@ export const obfuscatedCoordinates = (latlng, cacheKey = null) => { ? memoizedObfuscatedCoordinatesImpl(latlng, cacheKey) : obfuscatedCoordinatesImpl(latlng); }; + +/** + * Query the user's current location from the browser API + * + * @return {Promise} user's current location + */ +export const userLocation = () => + new Promise((resolve, reject) => { + const geolocationAvailable = 'geolocation' in navigator; + + if (!geolocationAvailable) { + reject(new Error('Geolocation not available in browser')); + return; + } + + const onSuccess = position => + resolve(new LatLng(position.coords.latitude, position.coords.longitude)); + + const onError = error => reject(error); + + navigator.geolocation.getCurrentPosition(onSuccess, onError); + });