【问题标题】:Sort NSManagedObjects from closest to farthest from current location将 NSManagedObject 从离当前位置最近到最远排序
【发布时间】:2012-05-27 19:29:48
【问题描述】:

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

  • (双)纬度
  • (双)经度

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

我已经搜索过类似的东西,但我还是有点困惑。

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

谢谢

【问题讨论】:

    标签: objective-c core-data nssortdescriptor


    【解决方案1】:

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

    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;
        }
    }
    

    正如我刚刚从 lnafziger 那里了解到的,您应该在此添加他所展示的有用的 hack/workaround¹。


    ¹从这个词中选择对你最积极的词

    【讨论】:

    • +1 这样做的好方法!请查看我对 distanceFromLocation 中的错误的回答以及获得正确值的解决方法。
    • 我看到了,但我不想偷它:)
    • 偷走。我更喜欢“解决方法”而不是破解,哈哈。 :)
    • 对我来说,“hack”是一个小而优雅的解决方案,而“workaround”是我做和讨厌的事情,因为营销人员已经强行设定了截止日期,我即将破解。
    • @lnafziger,请参阅我的编辑以获取有关 hack/workaround 的解决方案。
    【解决方案2】:

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

    这里有一篇关于一些转换方法的文章,具体取决于您想要接受的近似值:http://en.wikipedia.org/wiki/Geographical_distance

    【讨论】:

      【解决方案3】:

      嗯,你不能。

      无论如何,不​​仅仅是通过自行排序纬度/经度。 :)

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

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

      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;
      

      【讨论】:

      • 我很好奇为什么你需要计算两个位置之间距离的两倍,然后计算 distance1 / 2 + distance2 / 2。你能告诉我为什么吗?
      • 试一试:计算location1到location2的距离,并与location2到location1的距离进行比较。它们是不同的。 (这是 distanceFromLocation 中的一个已知错误。)奇怪的是,平均值 正确的值。
      • 我肯定会将我的答案与@TDeBailleul 结合起来,以获得最佳解决方案!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多