iphone - 使用我的 GPS 位置对位置的 NSMutableArray 进行排序

标签 iphone objective-c ios nsmutablearray core-location

我想对一个 NSMutableArray 进行排序,其中每一行都是一个 NSMutableDictionary,我的 GPS 位置来自 CoreLocation 框架。

这是我的 POI 数组的示例

arrayCampi = (
{
    cap = 28100;
    "cell_phone" = "";
    championship = "IBL 1D";
    citta = Novara;
    division = "";
    email = "";
    fax = 0321457933;
    indirizzo = "Via Patti, 14";
    latitude = "45.437174";
    league = "";
    longitude = "8.596029";
    name = "Comunale M. Provini";
    naz = Italy;
    prov = NO;
    reg = Piemonte;
    sport = B;
    surname = "Elettra Energia Novara 2000";
    telefono = 03211816389;
    webaddress = "http://www.novarabaseball.it/";
})

我需要用我的位置(纬度和经度)对这个数组进行排序,每行的字段“纬度”和“经度”以升序排列(第一行是离我最近的兴趣点)。

我试过这个解决方案但没有成功:

+ (NSMutableArray *)sortBallparkList:(NSMutableArray *)arrayCampi location:(CLLocation *)myLocation  {

    if ([arrayCampi count] == 0) {
        return arrayCampi;
    }

    if (myLocation.coordinate.latitude == 0.00 &&
        myLocation.coordinate.longitude == 0.00) {
        return arrayCampi;
    }

    NSMutableArray *sortedArray = [NSMutableArray arrayWithArray:arrayCampi];

    BOOL finito = FALSE;
    NSDictionary *riga1, *riga2;

    while (!finito) {
        for (int i = 0; i < [sortedArray count] - 1; i++) {

            finito = TRUE;
            riga1 = [sortedArray objectAtIndex: i];
            riga2 = [sortedArray objectAtIndex: i+1];

            CLLocationDistance distanceA = [myLocation distanceFromLocation:
                                            [[CLLocation alloc]initWithLatitude:[[riga1 valueForKey:@"latitude"] doubleValue]                                             
                                                                      longitude:[[riga1 valueForKey:@"longitude"] doubleValue]]];
            CLLocationDistance distanceB = [myLocation distanceFromLocation:
                                            [[CLLocation alloc]initWithLatitude:[[riga2 valueForKey:@"latitude"] doubleValue]
                                                                      longitude:[[riga2 valueForKey:@"longitude"] doubleValue]]];
            if (distanceA > distanceB) {
                [riga1 retain];
                [riga2 retain];

                [sortedArray replaceObjectAtIndex:i+1 withObject:riga2];
                [sortedArray replaceObjectAtIndex:i withObject:riga1];

                [riga1 release];
                [riga2 release];

                finito = FALSE;
            }
        }
    }

    return sortedArray;
}

任何人都可以帮助我,还有其他解决方案吗?

亚历克斯。

最佳答案

按纬度和经度排序不会为您提供距任何给定坐标最近的位置。作为近似值*),您可以使用毕达哥拉斯(您在高中时学过,还记得吗?):

float distance = sqrtf(powf((origLat-destLat),2)+powf((origLon-destLon), 2));

只需使用 @"distance" 键将其添加到您的字典中,然后使用

进行排序
NSArray *sorted = [arrayOfDictionaries sortedArrayUsingDescriptors:
     @[[NSSortDescriptor sortDescriptorWithKey:@"distance" ascending:YES]]];

*) 这是一个近似值,因为理论上两点之间的距离是椭圆体表面上的一条曲线。

关于iphone - 使用我的 GPS 位置对位置的 NSMutableArray 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12841428/

相关文章:

ios - 在 iOS 中使用 LDAP 进行身份验证

ios - SwiftUI:在同一行组合文本

iphone - -[CFString长度] : message sent to deallocated instance 0x3881940 when i scroll to near the bottom of a table on iphone

objective-c - NSException:如何添加 userData 并重新抛出?

objective-c - 方法 [[UIDevice currentDevice] uniqueIdentifier] 不再被允许,我需要一个替代方法

iphone - 我在 Xcode 4.5.2 中收到 'deprecated' 警告

iphone - 如何以编程方式确定我的应用程序正在 iPhone、iPad 或 iPhone 4 上运行?

iphone - 将唯一标识符从 IOS 设备发送到 Web 服务

iphone - 无法使用 __bridge 将 CFStringRef 转换为 NSString

ios - Xcode 6 : create new Objective C file messed up?