diff --git a/integration/test/ParseGeoPointTest.js b/integration/test/ParseGeoPointTest.js index add6d2088..ea61a2cbb 100644 --- a/integration/test/ParseGeoPointTest.js +++ b/integration/test/ParseGeoPointTest.js @@ -296,4 +296,37 @@ describe('Geo Point', () => { }) .fail(done.fail); }); + + it('works with withinCenterSphereKilometers queries', (done) => { + var sfo = new Parse.GeoPoint(37.6189722, -122.3748889); + var query = new Parse.Query(TestObject); + query.withinCenterSphereKilometers('location', sfo, 2000); + query.find().then(results => { + assert.equal(results.length, 2); + done(); + }) + .catch(err => done(err)); + }); + + it('works with withinCenterSphereMiles queries', (done) => { + var sfo = new Parse.GeoPoint(37.6189722, -122.3748889); + var query = new Parse.Query(TestObject); + query.withinCenterSphereMiles('location', sfo, 1243); + query.find().then(results => { + assert.equal(results.length, 2); + done(); + }) + .catch(err => done(err)); + }); + + it('works with withinCenterSphereRadians queries', (done) => { + var sfo = new Parse.GeoPoint(37.6189722, -122.3748889); + var query = new Parse.Query(TestObject); + query.withinCenterSphereRadians('location', sfo, 0.313922461); + query.find().then(results => { + assert.equal(results.length, 2); + done(); + }) + .catch(err => done(err)); + }); }); diff --git a/src/ParseQuery.js b/src/ParseQuery.js index 1591869ee..c3f56e138 100644 --- a/src/ParseQuery.js +++ b/src/ParseQuery.js @@ -1013,6 +1013,52 @@ export default class ParseQuery { return this._addCondition(key, '$geoWithin', { '$polygon': points }); } + /** + * Adds a constraint to the query that requires a particular key's + * coordinates be contained within the bounds of a given circle. + * + * @method withinCenterSphereRadians + * @param {String} key The key to be constrained. + * @param {ParseGeoPoint} point The reference Parse.GeoPoint that is used. + * @param {Number} radius The radius of the circle (in radians) + * @return {Parse.Query} Returns the query, so you can chain this call. + */ + withinCenterSphereRadians(key: string, point: ParseGeoPoint, radius: number): ParseQuery { + if (!(point instanceof ParseGeoPoint)) { + // Try to cast it as a GeoPoint + point = new ParseGeoPoint(point); + } + return this._addCondition(key, '$geoWithin', { '$centerSphere': [ point, radius ] }); + } + + /** + * Adds a constraint to the query that requires a particular key's + * coordinates be contained within the bounds of a given circle. + * + * @method withinCenterSphereMiles + * @param {String} key The key to be constrained. + * @param {ParseGeoPoint} point The reference Parse.GeoPoint that is used. + * @param {Number} radius The radius of the circle (in miles) + * @return {Parse.Query} Returns the query, so you can chain this call. + */ + withinCenterSphereMiles(key: string, point: ParseGeoPoint, radius: number): ParseQuery { + return this.withinCenterSphereRadians(key, point, radius / 3958.8); + } + + /** + * Adds a constraint to the query that requires a particular key's + * coordinates be contained within the bounds of a given circle. + * + * @method withinCenterSphereKilometers + * @param {String} key The key to be constrained. + * @param {ParseGeoPoint} point The reference Parse.GeoPoint that is used. + * @param {Number} radius The radius of the circle (in kilometers) + * @return {Parse.Query} Returns the query, so you can chain this call. + */ + withinCenterSphereKilometers(key: string, point: ParseGeoPoint, radius: number): ParseQuery { + return this.withinCenterSphereRadians(key, point, radius / 6371.0); + } + /** Query Orderings **/ /**