mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-26 14:57:18 +10:00
Add helper to get user's location
This commit is contained in:
parent
fd4c39d01d
commit
7e01233e52
1 changed files with 22 additions and 0 deletions
|
|
@ -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<LatLng>} 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);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue