-
-
Save junqueira/45ab8044f0b659318ce409e724a22260 to your computer and use it in GitHub Desktop.
Refatorei um exemplo de geolocation da w3schools: https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation
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
<!DOCTYPE html> | |
<html> | |
<body> | |
<p>Click the button to get your coordinates.</p> | |
<button id ='getCoordinates'>Try It</button> | |
<p id='coordinates'></p> | |
<script> | |
const addClickInGetCoordinates = () => | |
document.getElementById( 'getCoordinates' ) | |
.addEventListener( 'click', | |
showLocationIn( 'coordinates' ), | |
false); | |
const notExistsGeolocation = ( window ) => | |
!( window.navigator.geolocation ) | |
const geolocationIsntSupported = ( el ) => | |
el.innerHTML = 'Geolocation is not supported by this browser.' | |
const getCurrentPosition = ( geolocation, andShow, here ) => | |
geolocation.getCurrentPosition( andShow( here ) ) | |
const showPositionIn = ( el ) => ( position ) => | |
document.getElementById( el ).innerHTML = | |
'Latitude: ' + position.coords.latitude + '<br>' + | |
'Longitude: ' + position.coords.longitude | |
const showLocationIn = ( here ) => | |
notExistsGeolocation( window ) | |
? geolocationIsntSupported( here ) | |
: getCurrentPosition( navigator.geolocation, showPositionIn, here ) | |
const load = addClickInGetCoordinates | |
document.addEventListener('DOMContentLoaded', load, false); | |
</script> | |
</body> | |
</html> |
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
<!DOCTYPE html> | |
<html> | |
<body> | |
<p>Click the button to get your coordinates.</p> | |
<button onclick="getLocation()">Try It</button> | |
<p id="demo"></p> | |
<script> | |
var x = document.getElementById("demo"); | |
function getLocation() { | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(showPosition); | |
} else { | |
x.innerHTML = "Geolocation is not supported by this browser."; | |
} | |
} | |
function showPosition(position) { | |
x.innerHTML = "Latitude: " + position.coords.latitude + | |
"<br>Longitude: " + position.coords.longitude; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment