Skip to content

Instantly share code, notes, and snippets.

@Spoki4
Created January 16, 2019 21:43
Show Gist options
  • Save Spoki4/ac8004f496ffdb5376d3604c077fd787 to your computer and use it in GitHub Desktop.
Save Spoki4/ac8004f496ffdb5376d3604c077fd787 to your computer and use it in GitHub Desktop.
Get current user geolocation
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