ParseGeoPoint.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
  10. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
  11. /**
  12. * Creates a new GeoPoint with any of the following forms:<br>
  13. * <pre>
  14. * new GeoPoint(otherGeoPoint)
  15. * new GeoPoint(30, 30)
  16. * new GeoPoint([30, 30])
  17. * new GeoPoint({latitude: 30, longitude: 30})
  18. * new GeoPoint() // defaults to (0, 0)
  19. * </pre>
  20. * <p>Represents a latitude / longitude point that may be associated
  21. * with a key in a ParseObject or used as a reference point for geo queries.
  22. * This allows proximity-based queries on the key.</p>
  23. *
  24. * <p>Only one key in a class may contain a GeoPoint.</p>
  25. *
  26. * <p>Example:<pre>
  27. * var point = new Parse.GeoPoint(30.0, -20.0);
  28. * var object = new Parse.Object("PlaceObject");
  29. * object.set("location", point);
  30. * object.save();</pre></p>
  31. *
  32. * @alias Parse.GeoPoint
  33. */
  34. /* global navigator */
  35. class ParseGeoPoint {
  36. /**
  37. * @param {(number[] | object | number)} arg1 Either a list of coordinate pairs, an object with `latitude`, `longitude`, or the latitude or the point.
  38. * @param {number} arg2 The longitude of the GeoPoint
  39. */
  40. constructor(arg1, arg2) {
  41. (0, _defineProperty2.default)(this, "_latitude", void 0);
  42. (0, _defineProperty2.default)(this, "_longitude", void 0);
  43. if ((0, _isArray.default)(arg1)) {
  44. ParseGeoPoint._validate(arg1[0], arg1[1]);
  45. this._latitude = arg1[0];
  46. this._longitude = arg1[1];
  47. } else if (typeof arg1 === 'object') {
  48. ParseGeoPoint._validate(arg1.latitude, arg1.longitude);
  49. this._latitude = arg1.latitude;
  50. this._longitude = arg1.longitude;
  51. } else if (arg1 !== undefined && arg2 !== undefined) {
  52. ParseGeoPoint._validate(arg1, arg2);
  53. this._latitude = arg1;
  54. this._longitude = arg2;
  55. } else {
  56. this._latitude = 0;
  57. this._longitude = 0;
  58. }
  59. }
  60. /**
  61. * North-south portion of the coordinate, in range [-90, 90].
  62. * Throws an exception if set out of range in a modern browser.
  63. *
  64. * @property {number} latitude
  65. * @returns {number}
  66. */
  67. get latitude() {
  68. return this._latitude;
  69. }
  70. set latitude(val) {
  71. ParseGeoPoint._validate(val, this.longitude);
  72. this._latitude = val;
  73. }
  74. /**
  75. * East-west portion of the coordinate, in range [-180, 180].
  76. * Throws if set out of range in a modern browser.
  77. *
  78. * @property {number} longitude
  79. * @returns {number}
  80. */
  81. get longitude() {
  82. return this._longitude;
  83. }
  84. set longitude(val) {
  85. ParseGeoPoint._validate(this.latitude, val);
  86. this._longitude = val;
  87. }
  88. /**
  89. * Returns a JSON representation of the GeoPoint, suitable for Parse.
  90. *
  91. * @returns {object}
  92. */
  93. toJSON() {
  94. ParseGeoPoint._validate(this._latitude, this._longitude);
  95. return {
  96. __type: 'GeoPoint',
  97. latitude: this._latitude,
  98. longitude: this._longitude
  99. };
  100. }
  101. equals(other) {
  102. return other instanceof ParseGeoPoint && this.latitude === other.latitude && this.longitude === other.longitude;
  103. }
  104. /**
  105. * Returns the distance from this GeoPoint to another in radians.
  106. *
  107. * @param {Parse.GeoPoint} point the other Parse.GeoPoint.
  108. * @returns {number}
  109. */
  110. radiansTo(point) {
  111. const d2r = Math.PI / 180.0;
  112. const lat1rad = this.latitude * d2r;
  113. const long1rad = this.longitude * d2r;
  114. const lat2rad = point.latitude * d2r;
  115. const long2rad = point.longitude * d2r;
  116. const sinDeltaLatDiv2 = Math.sin((lat1rad - lat2rad) / 2);
  117. const sinDeltaLongDiv2 = Math.sin((long1rad - long2rad) / 2);
  118. // Square of half the straight line chord distance between both points.
  119. let a = sinDeltaLatDiv2 * sinDeltaLatDiv2 + Math.cos(lat1rad) * Math.cos(lat2rad) * sinDeltaLongDiv2 * sinDeltaLongDiv2;
  120. a = Math.min(1.0, a);
  121. return 2 * Math.asin(Math.sqrt(a));
  122. }
  123. /**
  124. * Returns the distance from this GeoPoint to another in kilometers.
  125. *
  126. * @param {Parse.GeoPoint} point the other Parse.GeoPoint.
  127. * @returns {number}
  128. */
  129. kilometersTo(point) {
  130. return this.radiansTo(point) * 6371.0;
  131. }
  132. /**
  133. * Returns the distance from this GeoPoint to another in miles.
  134. *
  135. * @param {Parse.GeoPoint} point the other Parse.GeoPoint.
  136. * @returns {number}
  137. */
  138. milesTo(point) {
  139. return this.radiansTo(point) * 3958.8;
  140. }
  141. /*
  142. * Throws an exception if the given lat-long is out of bounds.
  143. */
  144. static _validate(latitude, longitude) {
  145. if (isNaN(latitude) || isNaN(longitude) || typeof latitude !== 'number' || typeof longitude !== 'number') {
  146. throw new TypeError('GeoPoint latitude and longitude must be valid numbers');
  147. }
  148. if (latitude < -90.0) {
  149. throw new TypeError('GeoPoint latitude out of bounds: ' + latitude + ' < -90.0.');
  150. }
  151. if (latitude > 90.0) {
  152. throw new TypeError('GeoPoint latitude out of bounds: ' + latitude + ' > 90.0.');
  153. }
  154. if (longitude < -180.0) {
  155. throw new TypeError('GeoPoint longitude out of bounds: ' + longitude + ' < -180.0.');
  156. }
  157. if (longitude > 180.0) {
  158. throw new TypeError('GeoPoint longitude out of bounds: ' + longitude + ' > 180.0.');
  159. }
  160. }
  161. /**
  162. * Creates a GeoPoint with the user's current location, if available.
  163. *
  164. * @param {object} options The options.
  165. * @param {boolean} [options.enableHighAccuracy] A boolean value that indicates the application would like to receive the best possible results.
  166. * If true and if the device is able to provide a more accurate position, it will do so.
  167. * Note that this can result in slower response times or increased power consumption (with a GPS chip on a mobile device for example).
  168. * On the other hand, if false, the device can take the liberty to save resources by responding more quickly and/or using less power. Default: false.
  169. * @param {number} [options.timeout] A positive long value representing the maximum length of time (in milliseconds) the device is allowed to take in order to return a position.
  170. * The default value is Infinity, meaning that getCurrentPosition() won't return until the position is available.
  171. * @param {number} [options.maximumAge] A positive long value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return.
  172. * If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position.
  173. * If set to Infinity the device must return a cached position regardless of its age. Default: 0.
  174. * @static
  175. * @returns {Promise<Parse.GeoPoint>} User's current location
  176. */
  177. static current(options) {
  178. return new _promise.default((resolve, reject) => {
  179. navigator.geolocation.getCurrentPosition(location => {
  180. resolve(new ParseGeoPoint(location.coords.latitude, location.coords.longitude));
  181. }, reject, options);
  182. });
  183. }
  184. }
  185. var _default = exports.default = ParseGeoPoint;