Last active
September 22, 2021 20:59
-
-
Save joaosouz4dev/855efd0097f6ad08e9b6d1e132612a05 to your computer and use it in GitHub Desktop.
Capturando localização com javascript por IP e API de Localização
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<input name="laInicioMapa" id="laInicioMapa" value="-23.526217"> | |
<input name="loInicioMapa" id="loInicioMapa" value="-46.790832"> | |
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script> | |
<script src="./assets/js/custom.js"></script> | |
</body> | |
</html> |
This file contains hidden or 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
const GOOGLE_MAP_KEY = 'SUA CHAVE'; | |
function ipLookUp () { | |
$.ajax('http://ip-api.com/json') | |
.then( | |
function success(response) { | |
console.log('Os dados de localização do usuário são:', response); | |
console.log('O país do usuário é: ', response.country); | |
$('#laInicioMapa').val(response.lat) | |
$('#loInicioMapa').val(response.lon) | |
// caso queira trazer o endereço | |
getAddress(response.lat, response.lon) | |
}, | |
function fail(data, status) { | |
console.log('A solicitação falhou. Status retornado de', status); | |
} | |
); | |
} | |
function getAddress (latitude, longitude) { | |
$.ajax('https://maps.googleapis.com/maps/api/geocode/json?' + | |
'latlng=' + latitude + ',' + longitude + '&key=' + | |
GOOGLE_MAP_KEY) | |
.then( | |
function success (response) { | |
console.log('Os dados de endereço do usuário são ', response) | |
}, | |
function fail (status) { | |
console.log('A solicitação falhou. Status retornado de', status) | |
} | |
) | |
} | |
if ("geolocation" in navigator) { | |
// verifique se a geolocalização é suportada / ativada no navegador atual | |
navigator.geolocation.getCurrentPosition( | |
function success(position) { | |
// para quando a localização é um sucesso | |
console.log('latitude', position.coords.latitude, | |
'longitude', position.coords.longitude); | |
$('#laInicioMapa').val(position.coords.latitude) | |
$('loInicioMapa').val(position.coords.longitude) | |
// caso queira trazer o endereço | |
getAddress(position.coords.latitude, | |
position.coords.longitude) | |
}, | |
function error(error_message) { | |
// para quando a localização resulta em erro | |
console.error('Ocorreu um erro ao recuperar a localização', error_message) | |
ipLookUp() | |
}); | |
} else { | |
// geolocalização não é suportada | |
// obtenha sua localização de outra maneira | |
console.log('a geolocalização não está ativada neste navegador') | |
ipLookUp() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment