Skip to content

Instantly share code, notes, and snippets.

@psobot
Created January 13, 2013 06:39
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 psobot/1ccaaa4815fcc5279e43 to your computer and use it in GitHub Desktop.
Save psobot/1ccaaa4815fcc5279e43 to your computer and use it in GitHub Desktop.
function genericYiiLocation(callback) {
console.log('genericYiiLocation: Creating location handler');
this.longitude= '';
this.latitude= '';
this.accuracy= '';
var me = this;
if (Modernizr.geolocation) {
console.log('genericYiiLocation: Location supported');
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(locateSuccess, locateFail);
} else {
alert('genericYiiLocation: Geolocation is not supported in your current browser.');
callback(false);
}
} else {
alert ('genericYiiLocation: no native location support');
callback(false);
}
function locateSuccess(loc){
console.log('genericYiiLocation: storing location data');
me.longitude = loc.coords.longitude;
callback(true, me);
}
// Unsuccessful geolocation
function locateFail(geoPositionError) {
switch (geoPositionError.code) {
case 0: // UNKNOWN_ERROR
alert('An unknown error occurred, sorry');
break;
case 1: // PERMISSION_DENIED
alert('Permission to use Geolocation was denied');
break;
case 2: // POSITION_UNAVAILABLE
alert('Couldn\'t find you...');
break;
case 3: // TIMEOUT
alert('The Geolocation request took too long and timed out');
break;
default:
}
callback(false, geoPositionError);
}
}
// Usage:
genericYiiLocation(function(success, data){
if (!success) {
// Handle failure in here.
return;
} else {
this = data;
console.log("Got a longitude in callback: " + this.longitude);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment