Created
January 16, 2019 21:43
-
-
Save Spoki4/ac8004f496ffdb5376d3604c077fd787 to your computer and use it in GitHub Desktop.
Get current user geolocation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getLocation() { | |
return new Promise((resolve, reject) => { | |
navigator.geolocation.getCurrentPosition( | |
position => { | |
return resolve({ | |
latitude: position.coords.latitude, | |
longitude: position.coords.longitude | |
}) | |
}, | |
error => { | |
if (error.code === 1) { | |
return reject(new Error('PERMISSION_DENIED')) | |
} else if (error.code === 3) { | |
return reject(new Error('TIMEOUT')) | |
} else { | |
return reject(new Error('POSITION_UNAVAILABLE')) | |
} | |
}, | |
{ enableHighAccuracy: true, maximumAge: 60000, timeout: 5000 } | |
) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment