ParseGeoPoint.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  7. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  8. var ParseGeoPoint = function () {
  9. function ParseGeoPoint(arg1, arg2) {
  10. (0, _classCallCheck2.default)(this, ParseGeoPoint);
  11. if (Array.isArray(arg1)) {
  12. ParseGeoPoint._validate(arg1[0], arg1[1]);
  13. this._latitude = arg1[0];
  14. this._longitude = arg1[1];
  15. } else if (typeof arg1 === 'object') {
  16. ParseGeoPoint._validate(arg1.latitude, arg1.longitude);
  17. this._latitude = arg1.latitude;
  18. this._longitude = arg1.longitude;
  19. } else if (arg1 !== undefined && arg2 !== undefined) {
  20. ParseGeoPoint._validate(arg1, arg2);
  21. this._latitude = arg1;
  22. this._longitude = arg2;
  23. } else {
  24. this._latitude = 0;
  25. this._longitude = 0;
  26. }
  27. }
  28. (0, _createClass2.default)(ParseGeoPoint, [{
  29. key: "latitude",
  30. get: function () {
  31. return this._latitude;
  32. },
  33. set: function (val) {
  34. ParseGeoPoint._validate(val, this.longitude);
  35. this._latitude = val;
  36. }
  37. }, {
  38. key: "longitude",
  39. get: function () {
  40. return this._longitude;
  41. },
  42. set: function (val) {
  43. ParseGeoPoint._validate(this.latitude, val);
  44. this._longitude = val;
  45. }
  46. }, {
  47. key: "toJSON",
  48. value: function () {
  49. ParseGeoPoint._validate(this._latitude, this._longitude);
  50. return {
  51. __type: 'GeoPoint',
  52. latitude: this._latitude,
  53. longitude: this._longitude
  54. };
  55. }
  56. }, {
  57. key: "equals",
  58. value: function (other) {
  59. return other instanceof ParseGeoPoint && this.latitude === other.latitude && this.longitude === other.longitude;
  60. }
  61. }, {
  62. key: "radiansTo",
  63. value: function (point) {
  64. var d2r = Math.PI / 180.0;
  65. var lat1rad = this.latitude * d2r;
  66. var long1rad = this.longitude * d2r;
  67. var lat2rad = point.latitude * d2r;
  68. var long2rad = point.longitude * d2r;
  69. var sinDeltaLatDiv2 = Math.sin((lat1rad - lat2rad) / 2);
  70. var sinDeltaLongDiv2 = Math.sin((long1rad - long2rad) / 2);
  71. var a = sinDeltaLatDiv2 * sinDeltaLatDiv2 + Math.cos(lat1rad) * Math.cos(lat2rad) * sinDeltaLongDiv2 * sinDeltaLongDiv2;
  72. a = Math.min(1.0, a);
  73. return 2 * Math.asin(Math.sqrt(a));
  74. }
  75. }, {
  76. key: "kilometersTo",
  77. value: function (point) {
  78. return this.radiansTo(point) * 6371.0;
  79. }
  80. }, {
  81. key: "milesTo",
  82. value: function (point) {
  83. return this.radiansTo(point) * 3958.8;
  84. }
  85. }], [{
  86. key: "_validate",
  87. value: function (latitude, longitude) {
  88. if (isNaN(latitude) || isNaN(longitude) || typeof latitude !== 'number' || typeof longitude !== 'number') {
  89. throw new TypeError('GeoPoint latitude and longitude must be valid numbers');
  90. }
  91. if (latitude < -90.0) {
  92. throw new TypeError('GeoPoint latitude out of bounds: ' + latitude + ' < -90.0.');
  93. }
  94. if (latitude > 90.0) {
  95. throw new TypeError('GeoPoint latitude out of bounds: ' + latitude + ' > 90.0.');
  96. }
  97. if (longitude < -180.0) {
  98. throw new TypeError('GeoPoint longitude out of bounds: ' + longitude + ' < -180.0.');
  99. }
  100. if (longitude > 180.0) {
  101. throw new TypeError('GeoPoint longitude out of bounds: ' + longitude + ' > 180.0.');
  102. }
  103. }
  104. }, {
  105. key: "current",
  106. value: function () {
  107. return navigator.geolocation.getCurrentPosition(function (location) {
  108. return new ParseGeoPoint(location.coords.latitude, location.coords.longitude);
  109. });
  110. }
  111. }]);
  112. return ParseGeoPoint;
  113. }();
  114. var _default = ParseGeoPoint;
  115. exports.default = _default;