Created
February 20, 2011 12:33
-
-
Save rubenfonseca/835942 to your computer and use it in GitHub Desktop.
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
Biscates.Location = (function() { | |
var WATCH_ID; | |
var POSITION; | |
var GEOLOCATION_OPTIONS = { | |
enableHighAccuracy: true, | |
maximumAge: 600000, | |
timeout: 30000 // 30 seconds | |
}; | |
function _initGeolocation() { | |
var geo = navigator.geolocation; | |
if (typeof geo === 'undefined') { | |
// We don't have W3C geolocation. Try Gears. | |
if (typeof google !== 'undefined' && google.gears && | |
google.gears.factory.create) { | |
geo = google.gears.factory.create('beta.geolocation'); | |
} | |
} | |
// If we have a geo object with either of these approaches, proceed | |
// as planned. If not, try BlackBerry, and then fall back to Google | |
// Ajax API. | |
if (typeof geo !== 'undefined') { | |
WATCH_ID = geo.watchPosition( | |
_positionSucceeded, | |
_positionFailed, | |
GEOLOCATION_OPTIONS | |
); | |
} else { | |
_positionFailed(null); | |
} | |
} | |
function _init() { | |
if (!window.Biscates) return; | |
_initGeolocation(); | |
} | |
$(window).bind('load', _init); | |
function _positionSucceeded(position) { | |
POSITION = position; | |
$(window).trigger('positionSucceeded', null); | |
} | |
function _positionFailed(error) { | |
POSITION = null; | |
$(window).trigger('positionFailed', null); | |
} | |
return { | |
// Attempt to geolocate the user. | |
getLocation: function() { | |
var lat, lng; | |
var CL = google.loader.ClientLocation; | |
if (POSITION) { | |
var coords = POSITION.coords; | |
return [coords.latitude, coords.longitude]; | |
} else if (CL) { | |
return [CL.latitude, CL.longitude]; | |
} | |
// Fall back to downtown Austin. | |
return [30.26800, -97.74283]; | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment