Skip to content

Instantly share code, notes, and snippets.

@rubenfonseca
Created February 20, 2011 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubenfonseca/835942 to your computer and use it in GitHub Desktop.
Save rubenfonseca/835942 to your computer and use it in GitHub Desktop.
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