对用户位置排序 iOS
Sort user locations iOS
我有一个 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";
如何使用NSSortDescriptor
.
根据currentUserLat
和currentUserLng
整理出NSDictionary ALLINFO
以下是按位置排序的代码 long/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;
}
}];
希望对您有所帮助。
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)];
}];
我认为您通过为纬度和经度创建单独的数组来混淆人们。
如果我没理解错的话,您有一个 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;
}
}];
我有一个 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";
如何使用NSSortDescriptor
.
currentUserLat
和currentUserLng
整理出NSDictionary ALLINFO
以下是按位置排序的代码 long/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;
}
}];
希望对您有所帮助。
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)];
}];
我认为您通过为纬度和经度创建单独的数组来混淆人们。
如果我没理解错的话,您有一个 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;
}
}];