iPhone:根据位置sorting

我一直在iPhone应用程序,在哪里 – 我有一个NSMutableArray像下面的用户列表。

myMutableArray: ( { FirstName = Getsy; LastName = marie; Latitude = "30.237314"; Longitude = "-92.461008"; }, { FirstName = Angel; LastName = openza; Latitude = "30.260329"; Longitude = "-92.450414"; }, { FirstName = Sara; LastName = Hetzel; Latitude = "30.2584499"; Longitude = "-92.4135357"; } ) 

我需要通过计算纬度和经度来根据位于我附近的位置对用户进行sorting。 直到现在我还没有办到。 有人可以帮我提供一些样品吗?

更新:按照Mr.sch的build议,我在下面尝试。 请检查我的更新代码。 这很好吗?

  NSArray *orderedUsers = [myMutableArray sortedArrayUsingComparator:^(id a,id b) { NSArray *userA = (NSArray *)a; NSArray *userB = (NSArray *)b; CGFloat aLatitude = [[userA valueForKey:@"Latitude"] floatValue]; CGFloat aLongitude = [[userA valueForKey:@"Longitude"] floatValue]; CLLocation *participantALocation = [[CLLocation alloc] initWithLatitude:aLatitude longitude:aLongitude]; CGFloat bLatitude = [[userA valueForKey:@"Latitude"] floatValue]; CGFloat bLongitude = [[userA valueForKey:@"Longitude"] floatValue]; CLLocation *participantBLocation = [[CLLocation alloc] initWithLatitude:bLatitude longitude:bLongitude]; CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:locationCoordinates.latitude longitude:locationCoordinates.longitude]; CLLocationDistance distanceA = [participantALocation distanceFromLocation:myLocation]; CLLocationDistance distanceB = [participantBLocation distanceFromLocation:myLocation]; if (distanceA < distanceB) { return NSOrderedAscending; } else if (distanceA > distanceB) { return NSOrderedDescending; } else { return NSOrderedSame; } }]; 

谢谢!

 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; } }]; 

首先,您需要计算当前位置与其他用户的位置之间的距离。

在math方面,这是一个Wolfram | Alpha的例子

现在“编程盟友”,你可以使用CLLocation类,这里是一个例子:

 (CLLocationDistance)getDistanceFrom:(const CLLocation *)location 

但首先,您需要从您的经纬度创build位置对象。 您可以使用:

 (id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude 

您可以计算您的头寸与这些头寸的位置之间的距离(地理位置,而不是平面!) ,并按该值进行sorting。

 You may solve your problem in following way 1)Firstly store all above values in separate array Like latArray ,longArray,nameArray 2)Now get the distance between location(longArray,latArray) from your current location. Then store these distances in separate Array(distanceArray). //getDistance Between currentLocation and desired Location -(void *)getDistanceFromCurrentLocation{ for(int val=0;val<[latArray count];val++){ NSString *dis_From_Current_Location; dis_From_Current_Location =nil; CGFloat str_Longitude=[[defaults objectForKey:@"long"]floatValue]; CGFloat str_Latitude=[[defaults objectForKey:@"lati"]floatValue]; //Suppose this is your current location CGFloat lat1= [[latArray objectAtIndex:val]floatValue]; //these array for lat CGFloat long1=[[longArray objectAtIndex:val]floatValue]; //these array for longArray CLLocation *location1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1]; CLLocation *location2 = [[CLLocation alloc] initWithLatitude:str_Latitude longitude:str_Longitude]; CLLocationDistance dist=[location1 distanceFromLocation:location2]; NSLog(@"Distance i meters: %f", [location1 distanceFromLocation:location2]); long long v = llabs(dist/1000); dis_From_Current_Location=[NSString stringWithFormat:@"%lld km",v]; [location1 release]; [location2 release]; [distanceArray addObject: dis_From_Current_Location]; //distanceArray is Global NsMutableArray. } } 

现在您应该应用sorting方法(select,泡沫)来sorting距离。

有一点需要注意的是,当你对distanceArray进行sorting时,请根据distanceArray参见下面的代码来调整nameArray的值,以便对排列distanceArray的代码进行调整,并调整nameArray的值。

 -(void)getSoretdArray{ NSString * tempStr,*tempStr2; for(int i=0;i<[distanceArray count]; i++){ for(int j=i+1;j<[distanceArray count]; j++){ if([distanceArray objectAtIndex:j]>[distanceArray objectAtIndex:j+1]){ tempStr=[distanceArray objectAtIndex:j]; NSString* str= [distanceArray objectAtIndex:j+1]; [ distanceArray insertObject:str atIndex:j]; [distanceArray insertObject:tempStr atIndex:j+1] ; //also change the name of corresponding location. //you have to adjust the stored names in namArray for storing names of Corresponding Distances tempStr2=[nameArray objectAtIndex:j]; NSString* str1= [nameArray objectAtIndex:j+1]; [ nameArray insertObject:str1 atIndex:j]; [nameArray insertObject:tempStr2 atIndex:j+1] ; } } } 

}

 This will definitely work just try to use carefully 
 - (void)viewDidLoad { [super viewDidLoad]; // This Array Taken Globally List_of_locationsArray =[[NSMutableArray alloc]initWithObjects: @{@"latitude" : @"17.415045",@"logitude":@"78.421424"} ,@{@"latitude" : @"17.415045",@"logitude":@"78.421424"},@{@"latitude" : @"17.415045",@"logitude":@"78.421424"},@{@"latitude" : @"17.415045",@"logitude":@"78.421424"},@{@"latitude" : @"17.415045",@"logitude":@"78.421424"} ,nil]; } -(void)sortingLocationsArray{ // CLLocation* currentLocation =[[CLLocation alloc]initWithLatitude:[currentLatitude doubleValue] longitude:[currentLogitude doubleValue]]; CLLocation* currentLocation =[[CLLocation alloc]initWithLatitude: currentLatitudeHere longitude:CurrentLogHere]; NSMutableArray* tempLocationsArr = [[NSMutableArray alloc]initWithCapacity:[locationsArray count]]; for (int i=0; i<[locationsArray count]; i++) { CLLocationDegrees latValue = [[locationsArray[i] objectForKey:@"latitude"] doubleValue]; CLLocationDegrees longValue = [[locationsArray[i] objectForKey:@"logitude"] doubleValue]; CLLocation* location = [[CLLocation alloc]initWithLatitude:latValue longitude:longValue]; [tempLocationsArr addObject:location]; NSArray* sortLocationArry = [tempLocationsArr sortedArrayUsingComparator:^NSComparisonResult(CLLocation* location1, CLLocation* location2) { CLLocationDistance distA = [location1 distanceFromLocation:currentLocation]; CLLocationDistance distB = [location2 distanceFromLocation:currentLocation]; if (distA < distB) { return NSOrderedAscending; } else if ( distA > distB) { return NSOrderedDescending; } else { return NSOrderedSame; } }]; //ArrayAfterSorting is another mutable Array to Store Sorting Data [ArrayAfterSorting removeAllObjects]; [sortLocationArry enumerateObjectsUsingBlock:^(CLLocation* location, NSUInteger idx, BOOL *stop) { NSMutableDictionary *tempDict = [[NSMutableDictionary alloc]init]; [tempDict setObject:[NSString stringWithFormat:@"%f",location.coordinate.latitude] forKey:@"latitude"]; [tempDict setObject:[NSString stringWithFormat:@"%f",location.coordinate.longitude] forKey:@"logitude"]; [ArrayAfterSorting addObject:tempDict]; }]; NSLog(@"sortedArray : %@", ArrayAfterSorting); } }