Skip to content

Instantly share code, notes, and snippets.

@juehan
Created December 13, 2012 05:57
Show Gist options
  • Save juehan/4274393 to your computer and use it in GitHub Desktop.
Save juehan/4274393 to your computer and use it in GitHub Desktop.
Geolocation API in HTML5
<!doctype html>
<html>
<head>
<title>Geolocation API in HTML5</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
</head>
<body>
<p>Geolocation API in HTML5</p>
<button id="btnGeo">Show where i am</button>
<script type="text/javascript">
var p = $('p');
(function(){
//callback
function printGeoLocation(pos){
p.append("<p>Latitude: " + pos.coords.latitude + "</p>")
.append("<p>Longitude: " + pos.coords.longitude + "</p>")
.append("<p>Position accuracy: " + pos.coords.accuracy + "</p>")
.append("<p>Altitude: " + pos.coords.altitude + "</p>")
.append("<p>Heading: " + pos.coords.heading + "</p>")
.append("<p>Speed: " + pos.coords.speed + "</p>");
}
$('#btnGeo').on('click', function(){
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(printGeoLocation); //pass callback
} else {
alert("sorry, this browser doesn't support Geolocation data");
}
})
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment