Ios 对用户位置进行排序
我有一个Ios 对用户位置进行排序,ios,objective-c,nsarray,nsdictionary,Ios,Objective C,Nsarray,Nsdictionary,我有一个NSArray,其中包含用户所在位置的信息,即他们的纬度和经度。 我还得到了当前用户的位置,现在我想从NSArray中整理出纬度和经度列表,以便当前用户可以很容易地看到附近的用户 例如,我有以下信息 NSArray *ALLINFOLAT = [ALLINFO valueForKey:@"lat"] // having "40.880048", "40.749315", "40.749278", NSArray *ALLINFOLNG = [ALLINFO valueForKey:@"
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;
}
}];
NSArray*orderedUsers=[用户使用比较器进行排序:^(id a,id b){
用户*userA=(用户*)a;
User*userB=(User*)b;
CLLocationDistanceA=[userA.location getDistanceFromLocation:myLocation];
CLLocationDistanceB=[userB.location getDistanceFromLocation:myLocation];
if(距离A<距离B){
回传
}否则如果(距离A>距离B){
退而求其次;
}否则{
返回指定名称;
}
}];
------已编辑-----
以下代码只传递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;
}
}];
NSArray*orderedUsers=[self.shopsArray sortedarray usingcomparator:^(双行、双行、双行、双行)
{
CLLocation*usersA=[[CLLocation alloc]initWithLatitude:aLati经度:bLati];
CLLocation*usersB=[[CLLocation alloc]initWithLatitude:沿经度:bLong];
CLLocationDistance distA=[usersA distanceFromLocation:currentLocation];
CLLocationDistanceDistanceB=[usersB distanceFromLocation:currentLocation];
if(distAdistB){
返回(NSComparisonResult)或撤销;
}否则{
返回(NSComparisonResult)命令名;
}
}];
希望这对您有所帮助。我认为您创建的纬度和经度单独数组让人感到困惑
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;
}
}];
如果我理解正确的话,您有一个数组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;
}
}];
CLLocation*currentLocation=[[CLLocation alloc]initWithLatitude:78经度:87];
NSArray*orderedInfo=[ALLINFO SortedArray使用比较器:^(NSDictionary*object1,NSDictionary*object2)
{
CLLocation*location1=[[CLLocation alloc]initWithLatitude:[object1[@“lat”]doubleValue]经度:[object1[@“lng”]doubleValue]];
CLLocation*location2=[[CLLocation alloc]initWithLatitude:[object2[@“lat”]doubleValue]经度:[object2[@“lng”]doubleValue]];
CLLocationDistanceDistance1=[location1 distanceFromLocation:currentLocation];
CLLocationDistanceDistance2=[location2 distanceFromLocation:currentLocation];
如果(dist1dist2){
返回(NSComparisonResult)或撤销;
}否则{
返回(NSComparisonResult)命令名;
}
}];
什么是a和b?它们又长又宽?当然,我只想把lat和lng分类。对于这个干净的解决方案,我要发布其他信息,拇指upPlz加上这个,我怎样才能得到坐标?目前它的位置还可以,不用担心。我在等你sortLat
和sortLong
未定义。另外,我必须在NSARRY allinfo中进行排序。您不能修改allinfo lat和allinfo,因为它被定义为NSArray而不是NSMutableArray。再次查看我将修改答案。现在重试,我修改了AllInfo lat和AllInfo的定义,我还使用了removeAllObjects方法来避免内存泄漏。位置1和位置2是什么,其中我只有当前的lat和lng以及lat和lng列表。它们只是排序所需的一些临时变量。您能显示ALLINFO数组中的对象类型吗?ALLINFO是一个NSARRAY,它具有[ALLINFO valueForKey:@“lat”]
纬度和[ALLINFO valueForKey:@“lng”]
对于longitudeIf ALLINFO是一个NSARRAY,那么它包含可以通过索引而不是键访问的对象。我相信你的数组中包含NSDictionary。当您在数组上调用[ALLINFO valueForKey:@“lat”]时,它实际上会将调用转发给所有is dictionary对象,并返回一个仅提取lat值的新数组。
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;
}
}];