English 中文(简体)
从离当前位置最近至最远的 NNS 管理对象排序
原标题:Sort NSManagedObjects from closest to farthest from current location

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

  • (Double) latitude
  • (Double) longitude

现在,我想取回这些对象, 并根据它们与用户当前位置的比较程度进行排序。 我已经知道如何获得当前位置, 但我仍然无法找到的是如何根据两个属性对结果进行排序。

我在找类似的东西 但我还是有点困惑

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

谢谢 谢谢

最佳回答

与参照系统区块进行排序相当容易

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

正如我刚刚从Inafziger中学到的,你应该加上这个有用的黑客/工作1, 他正在展示的,这个。


1 < sub> 选择组成此单词, 该单词对您具有最积极的内涵 。

问题回答

您可能想要将长对/拉特对转换为点之间的地理距离,然后对单个属性进行排序。

本文提供一篇关于某些转换方法的文章,视您希望接受的近似值而定:http://en.wikipedia.org/wiki/Gegraphal_term

嗯,你不能这样做。

不只是通过自己来分拣时间/ 时间 。 : ) () :

您需要有一个包含距离您当前位置的属性。 您可以为此添加一个根据需要计算的短暂属性, 或者创建另一个距离( 可能比较容易) 的阵列 。

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

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;




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签