Skip to content

Instantly share code, notes, and snippets.

@jgarciaruiz
Created June 27, 2019 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgarciaruiz/bba07d263432ad1c72f5124d417a6cdb to your computer and use it in GitHub Desktop.
Save jgarciaruiz/bba07d263432ad1c72f5124d417a6cdb to your computer and use it in GitHub Desktop.
geolocation promise
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.'));
}
});
}
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