Skip to content

Instantly share code, notes, and snippets.

@EchoAbstract
Created February 14, 2012 03:54
Show Gist options
  • Save EchoAbstract/1823379 to your computer and use it in GitHub Desktop.
Save EchoAbstract/1823379 to your computer and use it in GitHub Desktop.
Geo Queries Demonstation
// This code assumes that you have a class with a description property,
// an address property and a coordinates property. To properly enable
// geo queries, makes sure to map the "description" property to _geoloc
// in your mapping function (eg, @"_geoloc", @"description" in the dictionary
// initializer).
myPlace1.description = @"2000 sq feed small company office";
myPlace1.address = @"1 Main St, Cambridge, MA";
myPlace1.coordinates = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:-71.083934],
[NSNumber numberWithDouble:42.362474], nil];
myPlace2.description = @"5000 sq feet in a commercial building";
myPlace2.address = @"1 Broadway, Cambridge, MA";
myPlace2.coordinates = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:-71.085438],
[NSNumber numberWithDouble:42.362688], nil];
myPlace3.description = @"10000 sq feet of office space”;
myPlace3.address = @"711 Atlantic Ave, Boston, MA";
myPlace3.coordinates = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:-71.056416],
[NSNumber numberWithDouble:42.351018], nil];
// The queries from the above code can be specified like so
// query={"_geoloc":{"$nearSphere":[-71,41]}}
KCSQuery *q1 = [KCSQuery queryOnField:@"coordinates"
usingConditional:kKCSNearSphere
forValue: [NSArray arrayWithObjects:
[NSNumber numberWithInt:-71],
[NSNumber numberWithInt:41], nil]];
// {"_geoloc":{"$nearSphere":[-71.05,42.35], "$maxDistance":"0.5"}}
KCSQuery *q2 = [KCSQuery queryOnField:@"coordinates"
usingConditionalsForValues:
kKCSNearSphere,
[NSArray arrayWithObjects:
[NSNumber numberWithFloat:-71.05],
[NSNumber numberWithFloat:42.35], nil],
kKCSMaxDistance,
[NSNumber numberWithFloat:0.5], nil];
//{ "$within" : { "$box" : [[-70, 44], [-72, 42]]}
NSArray *point1 = [NSArray arrayWithObjects:[NSNumber numberWithInt:-70],
[NSNumber numberWithInt:44], nil];
NSArray *point2 = [NSArray arrayWithObjects:[NSNumber numberWithInt:-72],
[NSNumber numberWithInt:42], nil];
NSArray *box = [NSArray arrayWithObjects:point1, point2, nil];
KCSQuery *q3 = [KCSQuery queryOnField:@"coordinates"
usingConditional:kKCSWithinBox
forValue:box];
// Get our places collection, assuming your class is called Places
KCSCollection *places = [KCSCollection collectionFromString:@"places"
ofClass:[Places class]];
// Fetch the first group of entities
places.query = q1;
[places fetchWithDelegate:self];
// Fetch the first group of entities
places.query = q2;
[places fetchWithDelegate:self];
// Fetch the first group of entities
places.query = q3;
[places fetchWithDelegate:self];
// Reset the query by setting it to an empty query.
places.query = [KCSQuery query];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment