ParseGeoPoint.js 6.3 KB

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