Created
April 4, 2017 23:25
-
-
Save suissa/12559e932c49829fcc1c29051c469701 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> |
Nada mal o refactor, apesar de ter um apelo funcional q ficou duca!, eu ainda acho a versão anterior bem mais legível, pq não precisou ser quebrada em vários fragmentos. Curto pacas funcional, no entanto observei q para pequenos exemplos ou POCs é boilerplate demais :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A função
notExistsGeolocation
pergunta se a propriedade existe no objeto window certo ? então pq não chamarisGeolocationSupported
e inverter o boolean, em minhas experiências usar ois
para provar a veracidade é maisbrainable
que o contrário. A funçãogeolocationIsntSupported
gera uma confusão, ela deveria ser chamar algo do tipowarnGeolocationIsntSupported
. A funçãoshowLocationIn
pode sershowLocationInElementId