Lungo.Device.Gps.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Geolocation API (HTML5 Feature)
  3. * Pending to final SPEC, now it's a Phonegap Wrapper
  4. * http://www.w3.org/TR/geolocation-API/
  5. *
  6. * @namespace LUNGO.Device
  7. * @class Gps
  8. *
  9. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  10. */
  11. LUNGO.Device.Gps = (function(lng, undefined) {
  12. var _position = null;
  13. var _watcher = null;
  14. var CALLBACK = {
  15. success: null,
  16. error: null
  17. };
  18. var GPS_OPTIONS = {
  19. enableHighAccuracy: false,
  20. timeout: 10000,
  21. maximumAge: 60000
  22. };
  23. /**
  24. * @ToDo
  25. *
  26. * @method get
  27. *
  28. * @param {Function} Callback
  29. */
  30. var get = function(callbacks, options) {
  31. if (_isReady()) {
  32. _bindCallbacks(callbacks);
  33. navigator.geolocation.getCurrentPosition(_success, _error, options);
  34. }
  35. };
  36. /**
  37. * @ToDo
  38. *
  39. * @method get
  40. *
  41. * @param {Function} @ToDo
  42. * @param {object} @ToDo
  43. */
  44. var watch = function(callbacks, options) {
  45. if (_isReady()) {
  46. _bindCallbacks(callbacks);
  47. _watcher = navigator.geolocation.watchPosition(_success, _error, options);
  48. }
  49. };
  50. /**
  51. * @ToDo
  52. *
  53. * @method position
  54. *
  55. */
  56. var position = function() {
  57. return _position;
  58. };
  59. /**
  60. * @ToDo
  61. *
  62. * @method clear
  63. */
  64. var stop = function() {
  65. _clearPosition();
  66. };
  67. /**
  68. * Console system to display messages when you are in debug mode.
  69. *
  70. * @method log
  71. *
  72. * @param {number} Severity based in (1)Log, (2)Warn, (>2)Error
  73. * @param {string} Message to show in console
  74. */
  75. var address = function(latitude, longitude, callback) {
  76. // @ToDo: Check Social Analytics RESTful
  77. };
  78. var _isReady = function() {
  79. if (navigator.geolocation) {
  80. _clearPosition();
  81. return true;
  82. } else {
  83. lng.core.log(3, 'Lungo.Device.Gps [ERROR]: navigator.geolocation is innacesible.')
  84. return false;
  85. }
  86. };
  87. var _clearPosition = function() {
  88. _position = null;
  89. if (_watcher) {
  90. navigator.geolocation.clearWatch(_watcher);
  91. _watcher = null;
  92. }
  93. };
  94. var _bindCallbacks = function(callbacks) {
  95. CALLBACK.success = (callbacks.success || null);
  96. CALLBACK.error = (callbacks.error || null);
  97. };
  98. var _success = function(position) {
  99. _position = position.coords;
  100. CALLBACK.success.call(CALLBACK.success, position);
  101. };
  102. var _error = function(error) {
  103. _clearPosition();
  104. CALLBACK.error.call(CALLBACK.error, error);
  105. };
  106. return {
  107. get: get,
  108. watch: watch,
  109. position: position,
  110. address: address,
  111. stop: stop
  112. }
  113. })(LUNGO);