gpt4 book ai didi

iphone - 如何从我的经纬度找出最近的用户?

转载 作者:行者123 更新时间:2023-11-28 22:40:30 25 4
gpt4 key购买 nike

我有一个应用程序,其中我有一个位置坐标。(即纬度和经度)。然后我有一个 friend 列表及其(纬度和经度)。

I need to sort my friends array according to the condition that who is nearest to me comes first

.我有他们的和我的经纬度。有人能帮我实现这个吗?

最佳答案

CLLocation *myLoc = [[CLLocation alloc] initWithLatitude:42.5000 longitude:1.5000];
NSArray *arr = [NSArray arrayWithObjects:
[[CLLocation alloc] initWithLatitude:42.5000 longitude:1.5000],
[[CLLocation alloc] initWithLatitude:24.0000 longitude:54.0000],
[[CLLocation alloc] initWithLatitude:33.0000 longitude:65.0000],
[[CLLocation alloc] initWithLatitude:17.0500 longitude:-61.8000],
[[CLLocation alloc] initWithLatitude:18.2500 longitude:-63.1667],
[[CLLocation alloc] initWithLatitude:41.0000 longitude:20.0000],
[[CLLocation alloc] initWithLatitude:40.0000 longitude:45.0000],
nil
];

CLLocation *nearestLoc = nil;
CLLocationDistance nearestDis = DBL_MAX;
for (CLLocation *loc in arr) {
CLLocationDistance distance = [myLoc distanceFromLocation:loc];
if (nearestDis > distance) {
nearestLoc = loc;
nearestDis = distance;
}
}

关于iphone - 如何从我的经纬度找出最近的用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14659808/

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