Ios UITableview中基于当前位置的排序距离
我如何根据与当前位置的距离对数组进行排序并在tableview中显示。当我使用以下代码进行排序时,我没有得到任何正确的结果,而是得到了随机距离的数组。有人能指导我解决此问题吗Ios UITableview中基于当前位置的排序距离,ios,objective-c,ios7.1,Ios,Objective C,Ios7.1,我如何根据与当前位置的距离对数组进行排序并在tableview中显示。当我使用以下代码进行排序时,我没有得到任何正确的结果,而是得到了随机距离的数组。有人能指导我解决此问题吗 -(void)sort_distance{ // Determine distance between current location and geofence location [distance_array removeAllObjects]; NSMutableDictionary *dict=[[NSMuta
-(void)sort_distance{
// Determine distance between current location and geofence location
[distance_array removeAllObjects];
NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
CLLocation *pinLocation;
CLLocation *userLocation;
for (int i=0; i<[self.geofences count]; i++) {
//dict =[self.geofences objectAtIndex:i];
CLLocationDegrees firstLat = [self.currentLatitude doubleValue];
CLLocationDegrees firstLong = [self.currentLongitude doubleValue];
CLLocationDegrees secondLat = [[[self.geofences objectAtIndex:i] objectForKey:@"lat"] doubleValue];
CLLocationDegrees secondLong = [[[self.geofences objectAtIndex:i] objectForKey:@"lon"] doubleValue];
pinLocation = [[CLLocation alloc]
initWithLatitude:secondLat
longitude:secondLong];
userLocation = [[CLLocation alloc]
initWithLatitude:firstLat
longitude:firstLong];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation]/1000;
//current distance is NSNumber
CLLocationDistance kilometers = distance /1000;
// or you can also use this..
distanceString = [[NSString alloc] initWithFormat: @"%.1f Km", kilometers];
NSLog(@"sa: %@", distanceString);
[distance_array addObject:distanceString];
[dict setObject:[NSNumber numberWithDouble:distance] forKey:@"newdistance"];
}
NSLog(@"distance array: %@", distance_array);
// Sorting the Geofence-Location depends on distance from current location
NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"newdistance" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sort];
sortedArray = [self.geofences sortedArrayUsingDescriptors:sortDescriptors];
}
-(void)排序距离{
//确定当前位置和土工围栏位置之间的距离
[distance_array removeAllObjects];
NSMutableDictionary*dict=[[NSMutableDictionary alloc]init];
CLLocation*pinLocation;
CLLocation*用户位置;
对于(int i=0;iCLLocationDistance是双精度类型,而您正在将其保存为整数类型。因此您将失去一些精度。请改用“[NSNumber numberWithDouble:”
这将按升序排序,因为降序返回的是取消排序而不是取消排序,反之亦然
-(CLLocationDistance )distanceOfCurrentPosition:(CLLocation *)userLocation WithPosition:(NSDictionary *)postition{
CLLocationDegrees secondLat = [postition[@"lat"] doubleValue];
CLLocationDegrees secondLong = [postition[@"lon"] doubleValue];
CLLocation *pinLocation = [[CLLocation alloc]
initWithLatitude:secondLat
longitude:secondLong];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation]/1000;
CLLocationDistance kilometers = distance /1000;
return kilometers;
}
-(void)sort_distance{
CLLocationDegrees firstLat = [self.currentLatitude doubleValue];
CLLocationDegrees firstLong = [self.currentLongitude doubleValue];
CLLocation *userLocation = [[CLLocation alloc]
initWithLatitude:firstLat
longitude:firstLong];
[_geofences sortUsingComparator:^NSComparisonResult(NSDictionary *position1, NSDictionary *position2) {
CLLocationDistance distanceWithPosition1 = [self distanceOfCurrentPosition:userLocation WithPosition:position1];
CLLocationDistance distanceWithPosition2 = [self distanceOfCurrentPosition:userLocation WithPosition:position2];
if (distanceWithPosition1> distanceWithPosition2) {
return NSOrderedDescending;
}else if(distanceWithPosition2 > distanceWithPosition1){
return NSOrderedAscending;
}else{
return NSOrderedSame;
}
}];
NSLog(@"Order :%@",_geofences);
}
-(void)排序距离{
//确定当前位置和土工围栏位置之间的距离
[distance_array removeAllObjects];
NSMutableDictionary*dict=[[NSMutableDictionary alloc]init];
CLLocation*pinLocation;
CLLocation*用户位置;
对于(int i=0;i请尝试这些方法对数组进行排序
-(NSArray *)sortArray:(NSArray *)arrayForSort forKey:(NSString *)key
{
return [arrayForSort sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if([obj1 isKindOfClass:[NSDictionary class]] && [obj2 isKindOfClass:[NSDictionary class]])
return [[obj1 objectForKey:key] compare:[obj2 objectForKey:key]];
return NSOrderedSame;
}];
}
就当是你的案子吧
self.geofenses=[self-sortArray:self.geofenses-forKey:@“newdistance”]
祝你今天愉快:)填充时,在您的geofences
数组中添加一个新键currentDistance
或类似的东西,并存储该点与当前位置之间的距离如何?然后根据该键的值对数组进行排序。每次用户更改位置时,您都可以轻松更新这些值。我假设您是我们正在使用某种机制来观察位置变化。在这种情况下,使用Comparator进行SorterDarray将是一个不错的选择。尝试一下。@N00B程序员:我已尝试根据您的评论添加新键,但没有任何更改。我无法理解距离\u数组
、dict
和SorterDarray
之间的关系。您是我们的哪个数组正在打印数据(在单元格中…
)?此外,排序是在您记录阵列后完成的。记录sortedArray
并告诉它的排序是什么。嘿@Chandu检查我的新答案。我希望能对我们有所帮助。,,,我尝试过这样做,但没有成功。请使用response type检查我的更新问题您正在self.geofenses中运行排序描述符,而您正在存储distance在一个单独的字典中。它将如何工作?它仅在一次排序距离正确时在tableview中不更新,并且始终第一个对象位于第一位。在重新加载表后,表中的位置也不会改变。AlSoSorDarray=[distance_array SortDarray UsingSelector:@selector(compare:)];NSLog(@“%@”,SortDarray);[\u tbl\u selectcard reloadData];以及tableview中的哪个数组计数??self.geofiner数组和距离标签am使用sortedArray
-(NSArray *)sortArray:(NSArray *)arrayForSort forKey:(NSString *)key
{
return [arrayForSort sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if([obj1 isKindOfClass:[NSDictionary class]] && [obj2 isKindOfClass:[NSDictionary class]])
return [[obj1 objectForKey:key] compare:[obj2 objectForKey:key]];
return NSOrderedSame;
}];
}