Repository navigation
Expand file tree
/
Copy pathgeopoint.js
More file actions
174 lines (165 loc) · 5.56 KB
/
Copy pathgeopoint.js
File metadata and controls
174 lines (165 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var _ = require('underscore');
/*global navigator: false */
module.exports = function(AV) {
/**
* Creates a new GeoPoint with any of the following forms:<br>
* @example
* new GeoPoint(otherGeoPoint)
* new GeoPoint(30, 30)
* new GeoPoint([30, 30])
* new GeoPoint({latitude: 30, longitude: 30})
* new GeoPoint() // defaults to (0, 0)
* @class
*
* <p>Represents a latitude / longitude point that may be associated
* with a key in a AVObject or used as a reference point for geo queries.
* This allows proximity-based queries on the key.</p>
*
* <p>Only one key in a class may contain a GeoPoint.</p>
*
* <p>Example:<pre>
* var point = new AV.GeoPoint(30.0, -20.0);
* var object = new AV.Object("PlaceObject");
* object.set("location", point);
* object.save();</pre></p>
*/
AV.GeoPoint = function(arg1, arg2) {
if (_.isArray(arg1)) {
AV.GeoPoint._validate(arg1[0], arg1[1]);
this.latitude = arg1[0];
this.longitude = arg1[1];
} else if (_.isObject(arg1)) {
AV.GeoPoint._validate(arg1.latitude, arg1.longitude);
this.latitude = arg1.latitude;
this.longitude = arg1.longitude;
} else if (_.isNumber(arg1) && _.isNumber(arg2)) {
AV.GeoPoint._validate(arg1, arg2);
this.latitude = arg1;
this.longitude = arg2;
} else {
this.latitude = 0;
this.longitude = 0;
}
// Add properties so that anyone using Webkit or Mozilla will get an error
// if they try to set values that are out of bounds.
var self = this;
if (this.__defineGetter__ && this.__defineSetter__) {
// Use _latitude and _longitude to actually store the values, and add
// getters and setters for latitude and longitude.
this._latitude = this.latitude;
this._longitude = this.longitude;
this.__defineGetter__('latitude', function() {
return self._latitude;
});
this.__defineGetter__('longitude', function() {
return self._longitude;
});
this.__defineSetter__('latitude', function(val) {
AV.GeoPoint._validate(val, self.longitude);
self._latitude = val;
});
this.__defineSetter__('longitude', function(val) {
AV.GeoPoint._validate(self.latitude, val);
self._longitude = val;
});
}
};
/**
* @lends AV.GeoPoint.prototype
* @property {float} latitude North-south portion of the coordinate, in range
* [-90, 90]. Throws an exception if set out of range in a modern browser.
* @property {float} longitude East-west portion of the coordinate, in range
* [-180, 180]. Throws if set out of range in a modern browser.
*/
/**
* Throws an exception if the given lat-long is out of bounds.
* @private
*/
AV.GeoPoint._validate = function(latitude, longitude) {
if (latitude < -90.0) {
throw new Error('AV.GeoPoint latitude ' + latitude + ' < -90.0.');
}
if (latitude > 90.0) {
throw new Error('AV.GeoPoint latitude ' + latitude + ' > 90.0.');
}
if (longitude < -180.0) {
throw new Error('AV.GeoPoint longitude ' + longitude + ' < -180.0.');
}
if (longitude > 180.0) {
throw new Error('AV.GeoPoint longitude ' + longitude + ' > 180.0.');
}
};
/**
* Creates a GeoPoint with the user's current location, if available.
* @return {Promise.<AV.GeoPoint>}
*/
AV.GeoPoint.current = () =>
new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(function(location) {
resolve(
new AV.GeoPoint({
latitude: location.coords.latitude,
longitude: location.coords.longitude,
})
);
}, reject);
});
_.extend(
AV.GeoPoint.prototype,
/** @lends AV.GeoPoint.prototype */ {
/**
* Returns a JSON representation of the GeoPoint, suitable for AV.
* @return {Object}
*/
toJSON: function() {
AV.GeoPoint._validate(this.latitude, this.longitude);
return {
__type: 'GeoPoint',
latitude: this.latitude,
longitude: this.longitude,
};
},
/**
* Returns the distance from this GeoPoint to another in radians.
* @param {AV.GeoPoint} point the other AV.GeoPoint.
* @return {Number}
*/
radiansTo: function(point) {
var d2r = Math.PI / 180.0;
var lat1rad = this.latitude * d2r;
var long1rad = this.longitude * d2r;
var lat2rad = point.latitude * d2r;
var long2rad = point.longitude * d2r;
var deltaLat = lat1rad - lat2rad;
var deltaLong = long1rad - long2rad;
var sinDeltaLatDiv2 = Math.sin(deltaLat / 2);
var sinDeltaLongDiv2 = Math.sin(deltaLong / 2);
// Square of half the straight line chord distance between both points.
var a =
sinDeltaLatDiv2 * sinDeltaLatDiv2 +
Math.cos(lat1rad) *
Math.cos(lat2rad) *
sinDeltaLongDiv2 *
sinDeltaLongDiv2;
a = Math.min(1.0, a);
return 2 * Math.asin(Math.sqrt(a));
},
/**
* Returns the distance from this GeoPoint to another in kilometers.
* @param {AV.GeoPoint} point the other AV.GeoPoint.
* @return {Number}
*/
kilometersTo: function(point) {
return this.radiansTo(point) * 6371.0;
},
/**
* Returns the distance from this GeoPoint to another in miles.
* @param {AV.GeoPoint} point the other AV.GeoPoint.
* @return {Number}
*/
milesTo: function(point) {
return this.radiansTo(point) * 3958.8;
},
}
);
};