5
我有一组数组中的词典。我在tableview中显示这些数据。在tableview中,我计算到每个对象的距离并显示在单元格中。按距离排序
数据需要能够根据递增的距离进行排序,很明显每个用户位置最好的方法是什么?我想把所有的字典分开并插入距离,然后放宽NSSortDescriptor,问题是,我太懒了。
现在,我只是用indexPath.row循环所有的数据并显示它。
我有一组数组中的词典。我在tableview中显示这些数据。在tableview中,我计算到每个对象的距离并显示在单元格中。按距离排序
数据需要能够根据递增的距离进行排序,很明显每个用户位置最好的方法是什么?我想把所有的字典分开并插入距离,然后放宽NSSortDescriptor,问题是,我太懒了。
现在,我只是用indexPath.row循环所有的数据并显示它。
您可以使用基于块的比较器api轻松进行排序。
NSArray *rawData = [NSArray arrayWithContentsOfFile:pathDoc];
rawData = [rawData sortedArrayUsingComparator: ^(id a, id b) {
CLLocationDistance dist_a= [[a objectsForKey:@"coordinate"] distanceFromLocation: userPosition];
CLLocationDistance dist_b= [[b objectsForKey:@"coordinate"] distanceFromLocation: userPosition];
if (dist_a < dist_b) {
return NSOrderedAscending;
} else if (dist_a > dist_b) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}];
你应该很容易找到calculate_distance(user_position, a);
与谷歌的实现。
我已经用distanceFromLocation计算了距离:但是对于一个位置,我需要NSArray中的字典中的经度和纬度。我使用for()和NSDictionary * dict = [array objectAtIndex:i]打开了这个循环,但是我如何使用基于块的比较器API匹配它? – MaikelS
看到我的编辑。要获得更多帮助,您必须发布代码。 – vikingosegundo
更多代码在这里:http://pastebin.com/dndv7dk1编辑了一下,以保持它的可读性。 – MaikelS