ParseGeoPoint.js 7.5 KB

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