ParseGeoPoint.js 6.5 KB

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