123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- "use strict";
- var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
- _Object$defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));
- var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
- class ParseGeoPoint {
-
- constructor(arg1, arg2) {
- (0, _defineProperty2.default)(this, "_latitude", void 0);
- (0, _defineProperty2.default)(this, "_longitude", void 0);
- if ((0, _isArray.default)(arg1)) {
- ParseGeoPoint._validate(arg1[0], arg1[1]);
- this._latitude = arg1[0];
- this._longitude = arg1[1];
- } else if (typeof arg1 === 'object') {
- ParseGeoPoint._validate(arg1.latitude, arg1.longitude);
- this._latitude = arg1.latitude;
- this._longitude = arg1.longitude;
- } else if (arg1 !== undefined && arg2 !== undefined) {
- ParseGeoPoint._validate(arg1, arg2);
- this._latitude = arg1;
- this._longitude = arg2;
- } else {
- this._latitude = 0;
- this._longitude = 0;
- }
- }
-
- get latitude() {
- return this._latitude;
- }
- set latitude(val) {
- ParseGeoPoint._validate(val, this.longitude);
- this._latitude = val;
- }
-
- get longitude() {
- return this._longitude;
- }
- set longitude(val) {
- ParseGeoPoint._validate(this.latitude, val);
- this._longitude = val;
- }
-
- toJSON() {
- ParseGeoPoint._validate(this._latitude, this._longitude);
- return {
- __type: 'GeoPoint',
- latitude: this._latitude,
- longitude: this._longitude
- };
- }
- equals(other) {
- return other instanceof ParseGeoPoint && this.latitude === other.latitude && this.longitude === other.longitude;
- }
-
- radiansTo(point) {
- const d2r = Math.PI / 180.0;
- const lat1rad = this.latitude * d2r;
- const long1rad = this.longitude * d2r;
- const lat2rad = point.latitude * d2r;
- const long2rad = point.longitude * d2r;
- const sinDeltaLatDiv2 = Math.sin((lat1rad - lat2rad) / 2);
- const sinDeltaLongDiv2 = Math.sin((long1rad - long2rad) / 2);
-
- let a = sinDeltaLatDiv2 * sinDeltaLatDiv2 + Math.cos(lat1rad) * Math.cos(lat2rad) * sinDeltaLongDiv2 * sinDeltaLongDiv2;
- a = Math.min(1.0, a);
- return 2 * Math.asin(Math.sqrt(a));
- }
-
- kilometersTo(point) {
- return this.radiansTo(point) * 6371.0;
- }
-
- milesTo(point) {
- return this.radiansTo(point) * 3958.8;
- }
-
- static _validate(latitude, longitude) {
- if (isNaN(latitude) || isNaN(longitude) || typeof latitude !== 'number' || typeof longitude !== 'number') {
- throw new TypeError('GeoPoint latitude and longitude must be valid numbers');
- }
- if (latitude < -90.0) {
- throw new TypeError('GeoPoint latitude out of bounds: ' + latitude + ' < -90.0.');
- }
- if (latitude > 90.0) {
- throw new TypeError('GeoPoint latitude out of bounds: ' + latitude + ' > 90.0.');
- }
- if (longitude < -180.0) {
- throw new TypeError('GeoPoint longitude out of bounds: ' + longitude + ' < -180.0.');
- }
- if (longitude > 180.0) {
- throw new TypeError('GeoPoint longitude out of bounds: ' + longitude + ' > 180.0.');
- }
- }
-
- static current(options) {
- return new _promise.default((resolve, reject) => {
- navigator.geolocation.getCurrentPosition(location => {
- resolve(new ParseGeoPoint(location.coords.latitude, location.coords.longitude));
- }, reject, options);
- });
- }
- }
- var _default = exports.default = ParseGeoPoint;
|