Skip to content

Commit ea3e75f

Browse files
authored
feat: localDatastore support for unsorted distance queries (#1570)
1 parent 4864d54 commit ea3e75f

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

integration/test/ParseLocalDatastoreTest.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,6 +2735,26 @@ function runTest(controller) {
27352735
assert.equal(objects.length, 1);
27362736
});
27372737

2738+
it(`${controller.name} supports withinKilometers`, async () => {
2739+
const object = new TestObject();
2740+
const firstPoint = new Parse.GeoPoint({ latitude: 40.0, longitude: -30.0 });
2741+
object.set({ location: firstPoint });
2742+
await object.save();
2743+
await object.pin();
2744+
2745+
const sorted = false;
2746+
const query = new Parse.Query(TestObject);
2747+
query.withinKilometers(
2748+
'location',
2749+
new Parse.GeoPoint({ latitude: 40.0, longitude: -30.0 }),
2750+
2,
2751+
sorted
2752+
);
2753+
query.fromLocalDatastore();
2754+
const results = await query.find();
2755+
assert.equal(results.length, 1);
2756+
});
2757+
27382758
it(`${controller.name} supports withinPolygon`, async () => {
27392759
const sacramento = new TestObject();
27402760
sacramento.set('location', new Parse.GeoPoint(38.52, -121.5));

src/OfflineQuery.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,25 @@ function matchesKeyConstraints(className, object, objects, key, constraints) {
513513
return true;
514514
}
515515
case '$geoWithin': {
516-
const points = compareTo.$polygon.map(geoPoint => [geoPoint.latitude, geoPoint.longitude]);
517-
const polygon = new ParsePolygon(points);
518-
return polygon.containsPoint(object[key]);
516+
if (compareTo.$polygon) {
517+
const points = compareTo.$polygon.map(geoPoint => [
518+
geoPoint.latitude,
519+
geoPoint.longitude,
520+
]);
521+
const polygon = new ParsePolygon(points);
522+
return polygon.containsPoint(object[key]);
523+
}
524+
if (compareTo.$centerSphere) {
525+
const [WGS84Point, maxDistance] = compareTo.$centerSphere;
526+
const centerPoint = new ParseGeoPoint({
527+
latitude: WGS84Point[1],
528+
longitude: WGS84Point[0],
529+
});
530+
const point = new ParseGeoPoint(object[key]);
531+
const distance = point.radiansTo(centerPoint);
532+
return distance <= maxDistance;
533+
}
534+
break;
519535
}
520536
case '$geoIntersects': {
521537
const polygon = new ParsePolygon(object[key].coordinates);

src/__tests__/OfflineQuery-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,15 @@ describe('OfflineQuery', () => {
471471
expect(matchesQuery(q.className, pt, [], q)).toBe(true);
472472
});
473473

474+
it('matches $centerSphere queries', () => {
475+
const pt = new ParseObject('Checkin');
476+
pt.set('location', new ParseGeoPoint(40, 40));
477+
478+
const q = new ParseQuery('Checkin');
479+
q.withinRadians('location', new ParseGeoPoint(30, 30), 0.3, false);
480+
expect(matchesQuery(q.className, pt, [], q)).toBe(true);
481+
});
482+
474483
it('matches $within queries', () => {
475484
const caltrainStation = new ParseObject('Checkin');
476485
caltrainStation

0 commit comments

Comments
 (0)