-2

现在我一次遍历一个数组并计算距离,如下所示:

- (void)calculateDistance
{

    ann = [dict objectForKey:@"Blue"];


        for(int i = 0; i < [ann count]; i++) {


            NSString *coordinates = [[ann objectAtIndex:i] objectForKey:@"Coordinates"];

            double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
            double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];

            // Calculating distance
            CLLocation *pinLocation = [[CLLocation alloc]
                                               initWithLatitude:realLatitude
                                               longitude:realLongitude];

            CLLocation *userLocation = [[CLLocation alloc]
                                                initWithLatitude:mapView.userLocation.coordinate.latitude
                                                longitude:mapView.userLocation.coordinate.longitude];

            CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];


            // Adding distance to dictionaries
            if (distance > 1000) {
            NSString *dist = [NSString stringWithFormat:@"%.2f km.", distance/1000];
            NSMutableDictionary *inDict = [[NSMutableDictionary alloc] init];
            inDict = [ann objectAtIndex:i];
            [inDict setValue:dist forKey:@"Distance"];
            }
            else{
            NSString *dist = [NSString stringWithFormat:@"%4.0f m.", distance];
            NSMutableDictionary *inDict = [[NSMutableDictionary alloc] init];
            inDict = [ann objectAtIndex:i];
            [inDict setValue:dist forKey:@"Distance"];
            }


        }

}

我的数据结构是:

数据结构

如何一次循环遍历所有数组?我有一个数组,其中包含我所有的名为“ resultArray ”的数组,但是这段代码不起作用:

ann = [dict objectForKey:resultArray];

NSLog(@"%@", resultArray);

2013-05-05 10:57:03.643 testApp[5708:907] (
    Black,
    Green,
    Orange,
    Blue,
    Darkblue
)
4

2 回答 2

2

我猜您想枚举存储在 resultArray 中的键并计算距离并将计算值添加到其中。

- (void)calculateDistance
{
    //Enumerates through resultArray
    for (NSString *key in resultArray) {

        //ann array is considered as an instance of NSMutableArray

        ann = dict[key];

        for(int i = 0; i < [ann count]; i++) {

            NSMutableDictionary *inDict = [ann[i] mutableCopy];

            NSString *coordinates = inDict[@"Coordinates"];

            NSArray *coordinateComponents = [coordinates componentsSeparatedByString:@","];

            double realLatitude  = [coordinateComponents[1] doubleValue];
            double realLongitude = [coordinateComponents[0] doubleValue];

            // Calculating distance
            CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:realLatitude
                                                                 longitude:realLongitude];

            CLLocation *userLocation = [[CLLocation alloc]
                                        initWithLatitude:mapView.userLocation.coordinate.latitude
                                        longitude:mapView.userLocation.coordinate.longitude];

            CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];


            // Adding distance to dictionaries
            if (distance > 1000) {
                NSString *dist = [NSString stringWithFormat:@"%.2f km.", distance/1000];
                [inDict setValue:dist forKey:@"Distance"];
            }
            else{
                NSString *dist = [NSString stringWithFormat:@"%4.0f m.", distance];
                [inDict setValue:dist forKey:@"Distance"];
            }
            //Inserting the modified values to the main array
            [ann replaceObjectAtIndex:i withObject:inDict];

        }

    }

}
于 2013-05-05T09:19:15.517 回答
0
ann = [dict objectForKey:resultArray];

for(NSArray *colourArray in ann)
{
    for(NSDictionary *itemDictionary in colourArray)
    {
       NSLog(@"Coordinates = %@",[itemDictionary objectForKey:@"Coordinates"]);

       NSLog(@"Name = %@",[itemDictionary objectForKey:@"Name"]);

       NSLog(@"Address = %@",[itemDictionary objectForKey:@"Address"]);
    }
}

希望它会帮助你。

于 2013-05-05T08:09:45.283 回答