Created
January 23, 2012 21:23
-
-
Save peelman/1665601 to your computer and use it in GitHub Desktop.
Javascript GPS Coordinate Object
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> | |
<head> | |
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> | |
<title>GPS Coords</title> | |
<script> | |
var map; | |
var info; | |
var latitude; | |
var longitude; | |
function Coordinate(decimal) { | |
this.base = decimal; | |
this.decimal = this.base; | |
this.isNeg = this.base < 0; | |
this.sign = this.isNeg ? "-" : "+"; | |
if (this.isNeg) { this.base = this.base * -1; } | |
this.degrees = Math.floor(this.base); | |
this.decMins = (this.base - this.degrees) * 60; | |
this.minutes = Math.floor(this.decMins); | |
this.decSecs = (this.decMins - this.minutes) * 60 | |
this.seconds = Math.floor(this.decSecs); | |
}; | |
function load() | |
{ | |
navigator.geolocation.getCurrentPosition(populateCoords); | |
} | |
function populateCoords(position) { | |
latitude = new Coordinate(position.coords.latitude); | |
longitude = new Coordinate(position.coords.longitude); | |
document.getElementById("dec-lat").innerHTML = latitude.decimal; | |
document.getElementById("dec-long").innerHTML = longitude.decimal; | |
document.getElementById("lat-sign").innerHTML = latitude.sign; | |
document.getElementById("long-sign").innerHTML = longitude.sign; | |
document.getElementById("lat-degrees").innerHTML = latitude.degrees; | |
document.getElementById("long-degrees").innerHTML = longitude.degrees | |
document.getElementById("lat-mins").innerHTML = latitude.minutes; | |
document.getElementById("long-mins").innerHTML = longitude.minutes; | |
document.getElementById('lat-secs').innerHTML = latitude.seconds; | |
document.getElementById('long-secs').innerHTML = longitude.seconds; | |
} | |
</script> | |
</head> | |
<body id="" onload="load(); return false;"> | |
<div id="coordinates"> | |
<div id="latitude"> | |
Lat: <span id="lat-sign"></span><span id="lat-degrees"></span>° <span id="lat-mins"></span>' <span id="lat-secs"></span>" | |
</div> | |
<div id="longitude"> | |
Long: <span id="long-sign"></span><span id="long-degrees"></span>° <span id="long-mins"></span>' <span id="long-secs"></span>" | |
</div> | |
<div id="dec-latitude"> | |
Lat: <span id="dec-lat"></span> | |
</div> | |
<div id="dec-longitude"> | |
Long: <span id="dec-long"></span> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment