gpt4 book ai didi

iPhone:根据位置排序

转载 作者:可可西里 更新时间:2023-11-01 06:19:53 25 4
gpt4 key购买 nike

我一直在开发一个 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";
}
)

我需要通过计算纬度和经度,根据位置附近的位置对用户进行排序。到目前为止,我无法实现这一目标。有人可以帮我提供一些 sample 吗?

更新:我正在按照 Mr.sch 的建议进行尝试,如下所示。请检查我更新的代码。可以吗?

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

关于iPhone:根据位置排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9308020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com