ParseGeoPoint.js 6.0 KB

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