ParseGeoPoint.js 5.6 KB

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