ParseGeoPoint.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. * @alias Parse.GeoPoint
  37. */
  38. /* global navigator */
  39. class ParseGeoPoint {
  40. /*:: _latitude: number;*/
  41. /*:: _longitude: number;*/
  42. /**
  43. * @param {(Number[]|Object|Number)} options Either a list of coordinate pairs, an object with `latitude`, `longitude`, or the latitude or the point.
  44. * @param {Number} longitude The longitude of the GeoPoint
  45. */
  46. constructor(arg1
  47. /*: Array<number> |
  48. { latitude: number; longitude: number } |
  49. number*/
  50. , arg2
  51. /*:: ?: number*/
  52. ) {
  53. if (Array.isArray(arg1)) {
  54. ParseGeoPoint._validate(arg1[0], arg1[1]);
  55. this._latitude = arg1[0];
  56. this._longitude = arg1[1];
  57. } else if (typeof arg1 === 'object') {
  58. ParseGeoPoint._validate(arg1.latitude, arg1.longitude);
  59. this._latitude = arg1.latitude;
  60. this._longitude = arg1.longitude;
  61. } else if (arg1 !== undefined && arg2 !== undefined) {
  62. ParseGeoPoint._validate(arg1, arg2);
  63. this._latitude = arg1;
  64. this._longitude = arg2;
  65. } else {
  66. this._latitude = 0;
  67. this._longitude = 0;
  68. }
  69. }
  70. /**
  71. * North-south portion of the coordinate, in range [-90, 90].
  72. * Throws an exception if set out of range in a modern browser.
  73. * @property latitude
  74. * @type 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. * @property longitude
  91. * @type Number
  92. */
  93. get longitude()
  94. /*: number*/
  95. {
  96. return this._longitude;
  97. }
  98. set longitude(val
  99. /*: number*/
  100. ) {
  101. ParseGeoPoint._validate(this.latitude, val);
  102. this._longitude = val;
  103. }
  104. /**
  105. * Returns a JSON representation of the GeoPoint, suitable for Parse.
  106. * @return {Object}
  107. */
  108. toJSON()
  109. /*: { __type: string; latitude: number; longitude: number }*/
  110. {
  111. ParseGeoPoint._validate(this._latitude, this._longitude);
  112. return {
  113. __type: 'GeoPoint',
  114. latitude: this._latitude,
  115. longitude: this._longitude
  116. };
  117. }
  118. equals(other
  119. /*: mixed*/
  120. )
  121. /*: boolean*/
  122. {
  123. return other instanceof ParseGeoPoint && this.latitude === other.latitude && this.longitude === other.longitude;
  124. }
  125. /**
  126. * Returns the distance from this GeoPoint to another in radians.
  127. * @param {Parse.GeoPoint} point the other Parse.GeoPoint.
  128. * @return {Number}
  129. */
  130. radiansTo(point
  131. /*: ParseGeoPoint*/
  132. )
  133. /*: number*/
  134. {
  135. const d2r = Math.PI / 180.0;
  136. const lat1rad = this.latitude * d2r;
  137. const long1rad = this.longitude * d2r;
  138. const lat2rad = point.latitude * d2r;
  139. const long2rad = point.longitude * d2r;
  140. const sinDeltaLatDiv2 = Math.sin((lat1rad - lat2rad) / 2);
  141. const sinDeltaLongDiv2 = Math.sin((long1rad - long2rad) / 2); // Square of half the straight line chord distance between both points.
  142. let a = sinDeltaLatDiv2 * sinDeltaLatDiv2 + Math.cos(lat1rad) * Math.cos(lat2rad) * sinDeltaLongDiv2 * sinDeltaLongDiv2;
  143. a = Math.min(1.0, a);
  144. return 2 * Math.asin(Math.sqrt(a));
  145. }
  146. /**
  147. * Returns the distance from this GeoPoint to another in kilometers.
  148. * @param {Parse.GeoPoint} point the other Parse.GeoPoint.
  149. * @return {Number}
  150. */
  151. kilometersTo(point
  152. /*: ParseGeoPoint*/
  153. )
  154. /*: number*/
  155. {
  156. return this.radiansTo(point) * 6371.0;
  157. }
  158. /**
  159. * Returns the distance from this GeoPoint to another in miles.
  160. * @param {Parse.GeoPoint} point the other Parse.GeoPoint.
  161. * @return {Number}
  162. */
  163. milesTo(point
  164. /*: ParseGeoPoint*/
  165. )
  166. /*: number*/
  167. {
  168. return this.radiansTo(point) * 3958.8;
  169. }
  170. /*
  171. * Throws an exception if the given lat-long is out of bounds.
  172. */
  173. static _validate(latitude
  174. /*: number*/
  175. , longitude
  176. /*: number*/
  177. ) {
  178. if (isNaN(latitude) || isNaN(longitude) || typeof latitude !== 'number' || typeof longitude !== 'number') {
  179. throw new TypeError('GeoPoint latitude and longitude must be valid numbers');
  180. }
  181. if (latitude < -90.0) {
  182. throw new TypeError('GeoPoint latitude out of bounds: ' + latitude + ' < -90.0.');
  183. }
  184. if (latitude > 90.0) {
  185. throw new TypeError('GeoPoint latitude out of bounds: ' + latitude + ' > 90.0.');
  186. }
  187. if (longitude < -180.0) {
  188. throw new TypeError('GeoPoint longitude out of bounds: ' + longitude + ' < -180.0.');
  189. }
  190. if (longitude > 180.0) {
  191. throw new TypeError('GeoPoint longitude out of bounds: ' + longitude + ' > 180.0.');
  192. }
  193. }
  194. /**
  195. * Creates a GeoPoint with the user's current location, if available.
  196. * Calls options.success with a new GeoPoint instance or calls options.error.
  197. * @static
  198. */
  199. static current() {
  200. return navigator.geolocation.getCurrentPosition(location => {
  201. return new ParseGeoPoint(location.coords.latitude, location.coords.longitude);
  202. });
  203. }
  204. }
  205. var _default = ParseGeoPoint;
  206. exports.default = _default;