Geo.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * A wrapper around HTML5 Geolocation with helper methods.
  3. *
  4. * @constructor
  5. */
  6. function Geo() {
  7. "use strict";
  8. /**
  9. * @type {Geolocation}
  10. */
  11. var geo = navigator.geolocation;
  12. this.getGeo = function () {
  13. return geo;
  14. };
  15. /**
  16. *
  17. * @param {MessageElement} area
  18. */
  19. this.showLocation = function (area) {
  20. var that = this;
  21. geo.getCurrentPosition(
  22. function (position) {
  23. area.set(that.getPositionMessage(position));
  24. },
  25. function (error) {
  26. area.set(error);
  27. }
  28. );
  29. };
  30. /**
  31. * Build a HTML-formatted representation of a position information.
  32. *
  33. * @param {Position} position
  34. * @returns {string}
  35. */
  36. this.getPositionMessage = function (position) {
  37. /**
  38. * @type {Coordinates}
  39. */
  40. var coords = position.coords;
  41. var date = new Date(position.timestamp);
  42. var message = "<p>Position: </p>\n" +
  43. "<ul>\n" +
  44. " <li>Timestamp: " + position.timestamp + " = " + date.toLocaleString() + "</li>\n" +
  45. " <li>Accuracy: " + coords.accuracy + " meters</li>\n" +
  46. " <li>Latitude: " + coords.latitude + "</li>" +
  47. " <li>Longitude: " + coords.longitude + "</li>" +
  48. " <li>Altitude: " + (coords.altitude ? coords.altitude : "unknown") + "</li>" +
  49. " <li>Altitude Accuracy: " + (coords.altitudeAccuracy ? coords.altitudeAccuracy : 0) + "</li>" +
  50. " <li>Heading: " + (coords.heading ? coords.heading : "unknown") + "</li>\n" +
  51. " <li>Speed: " + (coords.speed ? coords.speed : "unknown") + "</li>\n" +
  52. " </ul>\n";
  53. return message;
  54. };
  55. this.init = function () {
  56. geo = navigator.geolocation || false;
  57. };
  58. this.init();
  59. }