排序用户位置iOS

时间:2015-04-21 10:45:55

标签: ios objective-c nsarray nsdictionary

我有一个NSArray包含他们所在地的用户信息,即他们的纬度和经度。 我也得到了当前用户的位置,现在我想从NSArray中挑选纬度和经度列表,以便当前用户可以轻松地看到他的附近用户。

例如,我有以下信息。

NSArray *ALLINFOLAT = [ALLINFO valueForKey:@"lat"] 
// having "40.880048", "40.749315", "40.749278",
NSArray *ALLINFOLNG = [ALLINFO valueForKey:@"lng"] 
// having  "-77.145461", "-122.258591", "-122.320566"

NSString *currentUserLat = @"78";
NSString *currentUserLng = @"87";

如何根据NSDictionary ALLINFO&和{}分类currentUserLatcurrentUserLng使用NSSortDescriptor

3 个答案:

答案 0 :(得分:1)

NSMutableArray *ALLINFOLAT =@[@(40.880048), @(40.749315), @(40.749278)];
NSMutableArray *ALLINFOLNG =@[@(-77.145461), @(-122.258591), @(-122.320566)];
NSString *currentUserLat = @"78";
NSString *currentUserLng = @"87";
CLLocation* currentLocation =[[CLLocation alloc]initWithLatitude:[currentUserLat doubleValue] longitude:[currentUserLng doubleValue]];
NSMutableArray* locationArray = [[NSMutableArray alloc]initWithCapacity:[ALLINFOLNG count]];
for (int i=0; i<[ALLINFOLAT count]; i++) {
     CLLocationDegrees latValue = [ALLINFOLAT[i] doubleValue];
     CLLocationDegrees longValue = [ALLINFOLNG[i] doubleValue];
     CLLocation* location = [[CLLocation alloc]initWithLatitude:latValue longitude:longValue];
     [locationArray addObject:location];   
}
NSArray* sortLocationArry = [locationArray sortedArrayUsingComparator:^NSComparisonResult(CLLocation* location1, CLLocation* location2) {
            CLLocationDistance distA = [location1 distanceFromLocation:currentLocation];
            CLLocationDistance distB = [location2 distanceFromLocation:currentLocation];
            if (distA < distB) {
                return (NSComparisonResult)NSOrderedAscending;
            } else if ( distA > distB) {
                return (NSComparisonResult)NSOrderedDescending;
            } else {
                return (NSComparisonResult)NSOrderedSame;
            }
        }];

要获得经纬度排序,

[ALLINFOLAT removeAllObjects];
[ALLINFOLNG removeAllObjects];
[sortLocationArry enumerateObjectsUsingBlock:^(CLLocation* location, NSUInteger idx, BOOL *stop) {
   [ALLINFOLAT addObject:@(location.coordinate.latitude)];
   [ALLINFOLNG addObject:@(location.coordinate.longitude)];
}];

答案 1 :(得分:1)

我认为你通过为纬度和经度创建单独的数组来混淆人们。

如果我理解正确,你有一个数组ALLINFO,用于保存有关用户及其位置的信息,并且您希望根据与当前位置的距离对其进行排序。

您没有指定数组中的对象,因此我假设它们是字典。

以下是获取排序数组的方法:

CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude: 78 longitude: 87];

NSArray *orderedInfo = [ALLINFO sortedArrayUsingComparator: ^(NSDictionary *object1, NSDictionary *object2)
{
    CLLocation *location1 = [[CLLocation alloc] initWithLatitude: [object1[@"lat"] doubleValue] longitude: [object1[@"lng"] doubleValue]];
    CLLocation *location2 = [[CLLocation alloc] initWithLatitude: [object2[@"lat"] doubleValue] longitude: [object2[@"lng"] doubleValue]];

    CLLocationDistance dist1 = [location1 distanceFromLocation:currentLocation];
    CLLocationDistance dist2 = [location2 distanceFromLocation:currentLocation];

    if (dist1 < dist2) {
        return (NSComparisonResult)NSOrderedAscending;
    } else if ( dist1 > dist2) {
        return (NSComparisonResult)NSOrderedDescending;
    } else {
        return (NSComparisonResult)NSOrderedSame;
    }
}];

答案 2 :(得分:0)

以下是按位置/ lati排序的代码。

    NSArray *orderedUsers = [users sortedArrayUsingComparator:^(id a,id b) {
         User *userA = (User *)a;
         User *userB = (User *)b;
         CLLocationDistance distanceA = [userA.location getDistanceFromLocation:myLocation];
         CLLocationDistance distanceB = [userB.location getDistanceFromLocation:myLocation];
         if (distanceA < distanceB) {
             return NSOrderedAscending
         } else if (distanceA > distanceB) {
             return NSOrderedDescending;
         } else {
             return NSOrderedSame;
         }
    }];

--------已编辑--------

以下代码只传递你的long / lati的值。

NSArray *orderedUsers = [self.shopsArray sortedArrayUsingComparator: ^(double aLong, double bLong,double aLati, double bLati)
{
    CLLocation *usersA = [[CLLocation alloc] initWithLatitude: aLati longitude: bLati];
    CLLocation *usersB = [[CLLocation alloc] initWithLatitude: aLong longitude: bLong];

    CLLocationDistance distA = [usersA distanceFromLocation:currentLocation];
    CLLocationDistance distB = [usersB distanceFromLocation:currentLocation];

    if (distA < distB) {
        return (NSComparisonResult)NSOrderedAscending;
    } else if ( distA > distB) {
        return (NSComparisonResult)NSOrderedDescending;
    } else {
        return (NSComparisonResult)NSOrderedSame;
    }
}];

愿这对你有所帮助。