1 | 'use strict';
|
2 |
|
3 | var _promise = require('babel-runtime/core-js/promise');
|
4 |
|
5 | var _promise2 = _interopRequireDefault(_promise);
|
6 |
|
7 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
8 |
|
9 | var _ = require('underscore');
|
10 |
|
11 |
|
12 | module.exports = function (AV) {
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | AV.GeoPoint = function (arg1, arg2) {
|
36 | if (_.isArray(arg1)) {
|
37 | AV.GeoPoint._validate(arg1[0], arg1[1]);
|
38 | this.latitude = arg1[0];
|
39 | this.longitude = arg1[1];
|
40 | } else if (_.isObject(arg1)) {
|
41 | AV.GeoPoint._validate(arg1.latitude, arg1.longitude);
|
42 | this.latitude = arg1.latitude;
|
43 | this.longitude = arg1.longitude;
|
44 | } else if (_.isNumber(arg1) && _.isNumber(arg2)) {
|
45 | AV.GeoPoint._validate(arg1, arg2);
|
46 | this.latitude = arg1;
|
47 | this.longitude = arg2;
|
48 | } else {
|
49 | this.latitude = 0;
|
50 | this.longitude = 0;
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 | var self = this;
|
56 | if (this.__defineGetter__ && this.__defineSetter__) {
|
57 |
|
58 |
|
59 | this._latitude = this.latitude;
|
60 | this._longitude = this.longitude;
|
61 | this.__defineGetter__('latitude', function () {
|
62 | return self._latitude;
|
63 | });
|
64 | this.__defineGetter__('longitude', function () {
|
65 | return self._longitude;
|
66 | });
|
67 | this.__defineSetter__('latitude', function (val) {
|
68 | AV.GeoPoint._validate(val, self.longitude);
|
69 | self._latitude = val;
|
70 | });
|
71 | this.__defineSetter__('longitude', function (val) {
|
72 | AV.GeoPoint._validate(self.latitude, val);
|
73 | self._longitude = val;
|
74 | });
|
75 | }
|
76 | };
|
77 |
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 | AV.GeoPoint._validate = function (latitude, longitude) {
|
91 | if (latitude < -90.0) {
|
92 | throw new Error('AV.GeoPoint latitude ' + latitude + ' < -90.0.');
|
93 | }
|
94 | if (latitude > 90.0) {
|
95 | throw new Error('AV.GeoPoint latitude ' + latitude + ' > 90.0.');
|
96 | }
|
97 | if (longitude < -180.0) {
|
98 | throw new Error('AV.GeoPoint longitude ' + longitude + ' < -180.0.');
|
99 | }
|
100 | if (longitude > 180.0) {
|
101 | throw new Error('AV.GeoPoint longitude ' + longitude + ' > 180.0.');
|
102 | }
|
103 | };
|
104 |
|
105 | |
106 |
|
107 |
|
108 |
|
109 | AV.GeoPoint.current = function () {
|
110 | return new _promise2.default(function (resolve, reject) {
|
111 | navigator.geolocation.getCurrentPosition(function (location) {
|
112 | resolve(new AV.GeoPoint({
|
113 | latitude: location.coords.latitude,
|
114 | longitude: location.coords.longitude
|
115 | }));
|
116 | }, reject);
|
117 | });
|
118 | };
|
119 |
|
120 | _.extend(AV.GeoPoint.prototype,
|
121 | {
|
122 | |
123 |
|
124 |
|
125 |
|
126 | toJSON: function toJSON() {
|
127 | AV.GeoPoint._validate(this.latitude, this.longitude);
|
128 | return {
|
129 | __type: 'GeoPoint',
|
130 | latitude: this.latitude,
|
131 | longitude: this.longitude
|
132 | };
|
133 | },
|
134 |
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 | radiansTo: function radiansTo(point) {
|
141 | var d2r = Math.PI / 180.0;
|
142 | var lat1rad = this.latitude * d2r;
|
143 | var long1rad = this.longitude * d2r;
|
144 | var lat2rad = point.latitude * d2r;
|
145 | var long2rad = point.longitude * d2r;
|
146 | var deltaLat = lat1rad - lat2rad;
|
147 | var deltaLong = long1rad - long2rad;
|
148 | var sinDeltaLatDiv2 = Math.sin(deltaLat / 2);
|
149 | var sinDeltaLongDiv2 = Math.sin(deltaLong / 2);
|
150 |
|
151 | var a = sinDeltaLatDiv2 * sinDeltaLatDiv2 + Math.cos(lat1rad) * Math.cos(lat2rad) * sinDeltaLongDiv2 * sinDeltaLongDiv2;
|
152 | a = Math.min(1.0, a);
|
153 | return 2 * Math.asin(Math.sqrt(a));
|
154 | },
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 | kilometersTo: function kilometersTo(point) {
|
162 | return this.radiansTo(point) * 6371.0;
|
163 | },
|
164 |
|
165 | |
166 |
|
167 |
|
168 |
|
169 |
|
170 | milesTo: function milesTo(point) {
|
171 | return this.radiansTo(point) * 3958.8;
|
172 | }
|
173 | });
|
174 | }; |
\ | No newline at end of file |