var geo = (function(){ var geo = {} geo.fetch = function(){ navigator.geolocation.getCurrentPosition(geo.success, geo.error, {timeout: 15000}) } geo.success = function(position){ var lat_str = as_degrees( position.coords.latitude || 40.99167 ) var lng_str = as_degrees( position.coords.longitude || -74.07944 ) $(".latlng").html( lat_str + " " + lng_str ) } geo.error = function(error){ $(".latlng").html( "+40° 58' 90\" -74° 04' 46\"" ) } function as_degrees (n) { var s = "" if (n >= 0) s += "+" s += Math.floor(n) + "° " n = Math.abs(n) n %= 1 n *= 60 nn = Math.floor(n) if (nn < 10) nn = "0" + nn s += nn + "' " n %= 1 n *= 60 nn = Math.floor(n) if (nn < 10) nn = "0" + nn s += nn + '"' return s } return geo })()