Objective c 从距离当前位置最近到最远的位置对NSManagedObjects进行排序

Objective c 从距离当前位置最近到最远的位置对NSManagedObjects进行排序,objective-c,core-data,nssortdescriptor,Objective C,Core Data,Nssortdescriptor,我有一个用两个属性定义的核心数据模型 (双)纬度 (双)经度 现在,我想获取这些对象,并根据它们与用户当前位置的距离对它们进行排序。我已经知道如何获取当前位置,但我仍然不知道如何根据两个属性对结果进行排序 我一直在寻找类似的东西,但我还是有点困惑 如果有人能给我指出正确的方向那就太好了 谢谢您可能希望将长/宽对转换为点之间的地理距离,然后根据单个属性进行排序 这里有一篇关于一些转换方法的文章,具体取决于您希望接受的近似值:嗯,您不能 不管怎样,不仅仅是通过自己对lat/long进行排序。:)

我有一个用两个属性定义的核心数据模型

  • (双)纬度
  • (双)经度
现在,我想获取这些对象,并根据它们与用户当前位置的距离对它们进行排序。我已经知道如何获取当前位置,但我仍然不知道如何根据两个属性对结果进行排序

我一直在寻找类似的东西,但我还是有点困惑

如果有人能给我指出正确的方向那就太好了


谢谢

您可能希望将长/宽对转换为点之间的地理距离,然后根据单个属性进行排序

这里有一篇关于一些转换方法的文章,具体取决于您希望接受的近似值:

嗯,您不能

不管怎样,不仅仅是通过自己对lat/long进行排序。:)

您需要有一个包含到当前位置距离的属性。您可以通过添加根据需要计算的瞬态属性或创建另一个具有距离的数组(可能更容易)来实现这一点

要计算与当前位置的距离,请使用以下方法:

CLLocation *currentLocation   = // You said that you know how to get this.
CLLocation *storedLocation    = [CLLocation initWithLatitude:object.latitude 
                                                   longitude:object.longitude];
/*
 * Calculate distance in meters
 * Note that there is a bug in distanceFromLocation and it gives different
 * values depending on whether you are going TO or FROM a location. 
 * The correct distance is the average of the two:
 */
CLLocationDistance *distance1 = [currentLocation distanceFromLocation:storedLocation];
CLLocationDistance *distance2 = [storedLocation distanceFromLocation:currentLocation];
CLLocationDistance *distance  = distance1 / 2 + distance2 / 2;

使用比较器块进行排序非常容易

NSArray *positions = //all fetched positions
CLLocation *currentLocation   = // You said that you know how to get this.


positions = [positions sortedArrayUsingComparator: ^(id a, id b) {
    CLLocation *locationA    = [CLLocation initWithLatitude:a.latitude longitude:a.longitude];
    CLLocation *locationB    = [CLLocation initWithLatitude:b.latitude longitude:b.longitude];
    CLLocationDistance dist_a= [locationA distanceFromLocation: currentLocation];
    CLLocationDistance dist_b= [locationB distanceFromLocation: currentLocation];
    if ( dist_a < dist_b ) {
        return (NSComparisonResult)NSOrderedAscending;
    } else if ( dist_a > dist_b) {
        return (NSComparisonResult)NSOrderedDescending;
    } else {
        return (NSComparisonResult)NSOrderedSame;
    }
}
NSArray*positions=//所有获取的位置
CLLocation*currentLocation=//您说过您知道如何获取此信息。
位置=[使用比较器排列的位置:^(id a,id b){
CLLocation*locationA=[CLLocation INITWITHLATIONE:a.纬度经度:a.经度];
CLLocation*locationB=[CLLocation initWithLatitude:b.纬度经度:b.经度];
CLLocationDistance dist_a=[locationA DISTANCE FROMLOCATION:currentLocation];
CLLocationDistanceDist_b=[locationB distanceFromLocation:currentLocation];
if(距离a<距离b){
返回(NSComparisonResult)或删除;
}否则如果(距离a>距离b){
返回(NSComparisonResult)或撤销;
}否则{
返回(NSComparisonResult)命令名;
}
}
正如我刚刚从lnafziger那里学到的,您应该在这本书中添加有用的hack/workaround,他正在展示



从这个词中选择一个对你有最积极意义的词

我很好奇为什么你需要计算两个位置之间的距离的两倍,然后再计算距离1/2+距离2/2。你能告诉我为什么吗?试试看:计算位置1到位置2的距离,并将其与位置2的距离进行比较到位置1。它们是不同的。(这是距离位置已知的错误。)奇怪的是,平均值是正确的。我肯定会将我的答案与@tdebaileul结合起来,以获得最佳解决方案!+1很好的解决方法!请查看我的答案,尽管是关于距离位置的错误以及获得正确值的解决方法。我看到了,但我不想偷:)偷走。我更喜欢“变通”而不是黑客,哈哈:)对我来说,“黑客”是一个小而优雅的解决方案,其中“变通”是我所做和讨厌的事情,因为营销人员已经逼到了最后期限,我即将破解。@lnafziger,有关黑客/变通的解决方案,请参阅我的编辑。