Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 9, 2019 18:27
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 parzibyte/9a378f24cd9e7ea3d5c46c072890f0d9 to your computer and use it in GitHub Desktop.
Save parzibyte/9a378f24cd9e7ea3d5c46c072890f0d9 to your computer and use it in GitHub Desktop.
/**
Programado por Luis Cabrera Benito
____ _____ _ _ _
| _ \ | __ \ (_) | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___|
__/ | __/ |
|___/ |___/
Blog: https://parzibyte.me/blog
Ayuda: https://parzibyte.me/blog/contrataciones-ayuda/
Contacto: https://parzibyte.me/blog/contacto/
*/
const funcionInit = () => {
if (!"geolocation" in navigator) {
return alert("Tu navegador no soporta el acceso a la ubicación. Intenta con otro");
}
const $latitud = document.querySelector("#latitud"),
$longitud = document.querySelector("#longitud"),
$enlace = document.querySelector("#enlace");
const onUbicacionConcedida = ubicacion => {
console.log("Tengo la ubicación: ", ubicacion);
const coordenadas = ubicacion.coords;
$latitud.innerText = coordenadas.latitude;
$longitud.innerText = coordenadas.longitude;
$enlace.href = `https://www.google.com/maps/@${coordenadas.latitude},${coordenadas.longitude},20z`;
}
const onErrorDeUbicacion = err => {
$latitud.innerText = "Error obteniendo ubicación: " + err.message;
$longitud.innerText = "Error obteniendo ubicación: " + err.message;
console.log("Error obteniendo ubicación: ", err);
}
const opcionesDeSolicitud = {
enableHighAccuracy: true, // Alta precisión
maximumAge: 0, // No queremos caché
timeout: 5000 // Esperar solo 5 segundos
};
$latitud.innerText = "Cargando...";
$longitud.innerText = "Cargando...";
navigator.geolocation.getCurrentPosition(onUbicacionConcedida, onErrorDeUbicacion, opcionesDeSolicitud);
};
document.addEventListener("DOMContentLoaded", funcionInit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment