Created
June 27, 2019 11:01
-
-
Save jgarciaruiz/bba07d263432ad1c72f5124d417a6cdb to your computer and use it in GitHub Desktop.
geolocation promise
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
geolocateUser() { | |
return new Promise((resolve, reject) => { | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(position => { | |
let coordinates = {}; | |
coordinates.lat = position.coords.latitude; | |
coordinates.long = position.coords.longitude | |
resolve(coordinates); | |
}, positionError => { | |
let errorMsg; | |
switch (positionError.code) { | |
case positionError.TIMEOUT: | |
errorMsg = "Obtención de geolocalización abortada por no obtener respuesta."; | |
break; | |
case positionError.POSITION_UNAVAILABLE: | |
errorMsg = "Imposible obtener la información sobre tu posición."; | |
break; | |
case positionError.PERMISSION_DENIED: | |
errorMsg = "No se enviará tu localización de check-in si no das permiso para obtener tu localización."; | |
break; | |
default: | |
errorMsg = "Error de geolocalización."; | |
} | |
reject(Error(errorMsg)); | |
}); | |
} else { | |
reject(Error('Tu navegador no soporta la funcionalidad de geolocalización.')); | |
} | |
}); | |
} |
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
geolocateUser().then(resp => { | |
let coords = resp; | |
console.log(resp); | |
}).catch(err => { | |
console.log(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment