如何从我的纬度和经度中找出最近的用户?

时间:2013-02-02 07:47:47

标签: iphone ios ipad

我有一个应用程序,其中我有一个位置坐标。(即格子和经度)。然后我有一个朋友列表(lat& longs)。

  

我需要根据谁的条件对我的朋友数组进行排序   最接近我的是

。我有他们的和我的拉特和长。谁能帮我实现这个目标?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

嗯,这取决于你如何存储这些信息。如果你用postgis将它存储在postgres中,那么我很确定你可以按距离排序。如果没有,你可以抓住距离计算算法,只为所有朋友运行它,可能会缓存它,然后按距离排序。

相关问题