ParseGeoPoint.js 7.2 KB

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