使用 CloudKit CKQuery,您可以过滤距离(参见示例)。但是你怎么能按距离排序呢?像位置 ASC。位置上的排序描述符返回错误query.sortDescriptors = [NSSortDescriptor(key: "distanceToLocation", ascending: true)]
::
static func getNearbySpots(location : CLLocation, completionHandler: (spots : CKRecord[]) -> Void) {
let predicate = NSPredicate(format: "distanceToLocation:fromLocation:(location, %@) < 10000", location)
let queryOperation = CKQueryOperation(query: query)
var results : CKRecord[] = []
queryOperation.recordFetchedBlock = {
results += $0
}
queryOperation.queryCompletionBlock = {
if $1 {
NSLog($1.description,[])
abort()
} else {
completionHandler(spots: results)
}
}
self.publicDB().addOperation(queryOperation)
}