ParseGeoPoint.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. "use strict";
  2. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  3. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = void 0;
  8. var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));
  9. var _typeof2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/typeof"));
  10. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
  11. var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
  12. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
  13. /**
  14. * @flow
  15. */
  16. /**
  17. * Creates a new GeoPoint with any of the following forms:<br>
  18. * <pre>
  19. * new GeoPoint(otherGeoPoint)
  20. * new GeoPoint(30, 30)
  21. * new GeoPoint([30, 30])
  22. * new GeoPoint({latitude: 30, longitude: 30})
  23. * new GeoPoint() // defaults to (0, 0)
  24. * </pre>
  25. * <p>Represents a latitude / longitude point that may be associated
  26. * with a key in a ParseObject or used as a reference point for geo queries.
  27. * This allows proximity-based queries on the key.</p>
  28. *
  29. * <p>Only one key in a class may contain a GeoPoint.</p>
  30. *
  31. * <p>Example:<pre>
  32. * var point = new Parse.GeoPoint(30.0, -20.0);
  33. * var object = new Parse.Object("PlaceObject");
  34. * object.set("location", point);
  35. * object.save();</pre></p>
  36. *
  37. * @alias Parse.GeoPoint
  38. */
  39. /* global navigator */
  40. var ParseGeoPoint = /*#__PURE__*/function () {
  41. /**
  42. * @param {(number[] | object | number)} arg1 Either a list of coordinate pairs, an object with `latitude`, `longitude`, or the latitude or the point.
  43. * @param {number} arg2 The longitude of the GeoPoint
  44. */
  45. function ParseGeoPoint(arg1 /*: Array<number> | { latitude: number, longitude: number } | number*/, arg2 /*:: ?: number*/) {
  46. (0, _classCallCheck2.default)(this, ParseGeoPoint);
  47. (0, _defineProperty2.default)(this, "_latitude", void 0);
  48. (0, _defineProperty2.default)(this, "_longitude", void 0);
  49. if ((0, _isArray.default)(arg1)) {
  50. ParseGeoPoint._validate(arg1[0], arg1[1]);
  51. this._latitude = arg1[0];
  52. this._longitude = arg1[1];
  53. } else if ((0, _typeof2.default)(arg1) === 'object') {
  54. ParseGeoPoint._validate(arg1.latitude, arg1.longitude);
  55. this._latitude = arg1.latitude;
  56. this._longitude = arg1.longitude;
  57. } else if (arg1 !== undefined && arg2 !== undefined) {
  58. ParseGeoPoint._validate(arg1, arg2);
  59. this._latitude = arg1;
  60. this._longitude = arg2;
  61. } else {
  62. this._latitude = 0;
  63. this._longitude = 0;
  64. }
  65. }
  66. /**
  67. * North-south portion of the coordinate, in range [-90, 90].
  68. * Throws an exception if set out of range in a modern browser.
  69. *
  70. * @property {number} latitude
  71. * @returns {number}
  72. */
  73. (0, _createClass2.default)(ParseGeoPoint, [{
  74. key: "latitude",
  75. get: function () /*: number*/{
  76. return this._latitude;
  77. },
  78. set: function (val /*: number*/) {
  79. ParseGeoPoint._validate(val, this.longitude);
  80. this._latitude = val;
  81. }
  82. /**
  83. * East-west portion of the coordinate, in range [-180, 180].
  84. * Throws if set out of range in a modern browser.
  85. *
  86. * @property {number} longitude
  87. * @returns {number}
  88. */
  89. }, {
  90. key: "longitude",
  91. get: function () /*: number*/{
  92. return this._longitude;
  93. },
  94. set: function (val /*: number*/) {
  95. ParseGeoPoint._validate(this.latitude, val);
  96. this._longitude = val;
  97. }
  98. /**
  99. * Returns a JSON representation of the GeoPoint, suitable for Parse.
  100. *
  101. * @returns {object}
  102. */
  103. }, {
  104. key: "toJSON",
  105. value: function () /*: { __type: string, latitude: number, longitude: number }*/{
  106. ParseGeoPoint._validate(this._latitude, this._longitude);
  107. return {
  108. __type: 'GeoPoint',
  109. latitude: this._latitude,
  110. longitude: this._longitude
  111. };
  112. }
  113. }, {
  114. key: "equals",
  115. value: function (other /*: mixed*/) /*: boolean*/{
  116. return other instanceof ParseGeoPoint && this.latitude === other.latitude && this.longitude === other.longitude;
  117. }
  118. /**
  119. * Returns the distance from this GeoPoint to another in radians.
  120. *
  121. * @param {Parse.GeoPoint} point the other Parse.GeoPoint.
  122. * @returns {number}
  123. */
  124. }, {
  125. key: "radiansTo",
  126. value: function (point /*: ParseGeoPoint*/) /*: number*/{
  127. var d2r = Math.PI / 180.0;
  128. var lat1rad = this.latitude * d2r;
  129. var long1rad = this.longitude * d2r;
  130. var lat2rad = point.latitude * d2r;
  131. var long2rad = point.longitude * d2r;
  132. var sinDeltaLatDiv2 = Math.sin((lat1rad - lat2rad) / 2);
  133. var sinDeltaLongDiv2 = Math.sin((long1rad - long2rad) / 2);
  134. // Square of half the straight line chord distance between both points.
  135. var a = sinDeltaLatDiv2 * sinDeltaLatDiv2 + Math.cos(lat1rad) * Math.cos(lat2rad) * sinDeltaLongDiv2 * sinDeltaLongDiv2;
  136. a = Math.min(1.0, a);
  137. return 2 * Math.asin(Math.sqrt(a));
  138. }
  139. /**
  140. * Returns the distance from this GeoPoint to another in kilometers.
  141. *
  142. * @param {Parse.GeoPoint} point the other Parse.GeoPoint.
  143. * @returns {number}
  144. */
  145. }, {
  146. key: "kilometersTo",
  147. value: function (point /*: ParseGeoPoint*/) /*: number*/{
  148. return this.radiansTo(point) * 6371.0;
  149. }
  150. /**
  151. * Returns the distance from this GeoPoint to another in miles.
  152. *
  153. * @param {Parse.GeoPoint} point the other Parse.GeoPoint.
  154. * @returns {number}
  155. */
  156. }, {
  157. key: "milesTo",
  158. value: function (point /*: ParseGeoPoint*/) /*: number*/{
  159. return this.radiansTo(point) * 3958.8;
  160. }
  161. /*
  162. * Throws an exception if the given lat-long is out of bounds.
  163. */
  164. }], [{
  165. key: "_validate",
  166. value: function (latitude /*: number*/, longitude /*: number*/) {
  167. if (isNaN(latitude) || isNaN(longitude) || typeof latitude !== 'number' || typeof longitude !== 'number') {
  168. throw new TypeError('GeoPoint latitude and longitude must be valid numbers');
  169. }
  170. if (latitude < -90.0) {
  171. throw new TypeError('GeoPoint latitude out of bounds: ' + latitude + ' < -90.0.');
  172. }
  173. if (latitude > 90.0) {
  174. throw new TypeError('GeoPoint latitude out of bounds: ' + latitude + ' > 90.0.');
  175. }
  176. if (longitude < -180.0) {
  177. throw new TypeError('GeoPoint longitude out of bounds: ' + longitude + ' < -180.0.');
  178. }
  179. if (longitude > 180.0) {
  180. throw new TypeError('GeoPoint longitude out of bounds: ' + longitude + ' > 180.0.');
  181. }
  182. }
  183. /**
  184. * Creates a GeoPoint with the user's current location, if available.
  185. *
  186. * @static
  187. * @returns {Parse.GeoPoint} User's current location
  188. */
  189. }, {
  190. key: "current",
  191. value: function () {
  192. return navigator.geolocation.getCurrentPosition(function (location) {
  193. return new ParseGeoPoint(location.coords.latitude, location.coords.longitude);
  194. });
  195. }
  196. }]);
  197. return ParseGeoPoint;
  198. }();
  199. var _default = ParseGeoPoint;
  200. exports.default = _default;