Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. var geoloc;
  2.  
  3. var successful = function (position) {
  4. geoloc = {
  5. longitude: position.coords.longitude,
  6. latitude: position.coords.latitude
  7. };
  8. };
  9.  
  10. var getLocation = function () {
  11. navigator.geolocation.getCurrentPosition(successful, function () {
  12. alert("fail");
  13. });
  14.  
  15. return geoloc;
  16. };
  17.  
  18. var getLocation = function(callback){
  19. navigator.geolocation.getCurrentPosition(function(pos){
  20. succesfull(pos);
  21. typeof callback === 'function' && callback(geoloc);
  22. }, function(){
  23. alert("fail");
  24. });
  25. };
  26.  
  27. getLocation(function(pos){
  28. console.log(pos.longitude, pos.latitude);
  29. });
  30.  
  31. // create a new deferred object
  32. var deferred = $.Deferred();
  33.  
  34. var success = function (position) {
  35. // resolve the deferred with your object as the data
  36. deferred.resolve({
  37. longitude: position.coords.longitude,
  38. latitude: position.coords.latitude
  39. });
  40. };
  41.  
  42. var fail = function () {
  43. // reject the deferred with an error message
  44. deferred.reject('failed!');
  45. };
  46.  
  47. var getLocation = function () {
  48. navigator.geolocation.getCurrentPosition(success, fail);
  49.  
  50. return deferred.promise(); // return a promise
  51. };
  52.  
  53. // then you would use it like this:
  54. getLocation().then(
  55. function (location) {
  56. // success, location is the object you passed to resolve
  57. },
  58. function (errorMessage) {
  59. // fail, errorMessage is the string you passed to reject
  60. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement