Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var geoloc;
- var successful = function (position) {
- geoloc = {
- longitude: position.coords.longitude,
- latitude: position.coords.latitude
- };
- };
- var getLocation = function () {
- navigator.geolocation.getCurrentPosition(successful, function () {
- alert("fail");
- });
- return geoloc;
- };
- var getLocation = function(callback){
- navigator.geolocation.getCurrentPosition(function(pos){
- succesfull(pos);
- typeof callback === 'function' && callback(geoloc);
- }, function(){
- alert("fail");
- });
- };
- getLocation(function(pos){
- console.log(pos.longitude, pos.latitude);
- });
- // create a new deferred object
- var deferred = $.Deferred();
- var success = function (position) {
- // resolve the deferred with your object as the data
- deferred.resolve({
- longitude: position.coords.longitude,
- latitude: position.coords.latitude
- });
- };
- var fail = function () {
- // reject the deferred with an error message
- deferred.reject('failed!');
- };
- var getLocation = function () {
- navigator.geolocation.getCurrentPosition(success, fail);
- return deferred.promise(); // return a promise
- };
- // then you would use it like this:
- getLocation().then(
- function (location) {
- // success, location is the object you passed to resolve
- },
- function (errorMessage) {
- // fail, errorMessage is the string you passed to reject
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement