Wojtek
Wojtek

Reputation: 1044

CloudKit locations closest to point

I'm looking for a query for CloudKit that could return me objects with locations closest to mine, sorted by the distance. For now I can get locations within certain distance from my point.

NSPredicate(format: "distanceToLocation:fromLocation:(%K,%@) < %f","Location",location,radiusInKilometers)

But It's not what I need. What I'm looking for are locations sorted from the closest one, for me the radius doesn't matter, so solution to use this query and then sort results on my device is not good for me. Any help?

Upvotes: 4

Views: 669

Answers (1)

Dave Browning
Dave Browning

Reputation: 1276

You could use the CKLocationSortDescriptor in your sortDescriptors and combine that with resultsLimit. That will let you sort by location closest to the point you provide and get a specific number of results (e.g. 5).

queryOperation.sortDescriptors = [CKLocationSortDescriptor(key: "location", relativeLocation: location)]
queryOperation.resultsLimit = 5

Upvotes: 3

Related Questions