4

使用 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)
    }
4

1 回答 1

6

好的,找到了。有一个 CKLocationSortDescriptor,您可以在其中指定位置。

 query.sortDescriptors = [CKLocationSortDescriptor(key: "location", relativeLocation: location)]
于 2014-06-23T07:06:04.063 回答