-
-
Save psobot/1ccaaa4815fcc5279e43 to your computer and use it in GitHub Desktop.
An elaboration on an answer for SO question #14300700. (http://stackoverflow.com/questions/14300700/empty-variables-when-calling-javascript-function/14300733)
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
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