-1

現在の場所から目的の場所までの距離を取得するためにforループを使用しています。現在の場所から目的地までのすべての距離を含む配列が欲しいのですが。

for (i = 0; i < [Arr_Lat count]; i++)  
{
    NSString *latst1 = [[NSString alloc]initWithFormat:[Arr_Lat objectAtIndex:i]];
    NSString *longst1 = [[NSString alloc]initWithFormat:[Arr_Long objectAtIndex:i]];

    NSLog(@"First lat : %@",latst1);
    NSLog(@"First  ong : %@",longst1);

    double Doblat1 = [latst1 doubleValue];
    double Doblong1 = [longst1 doubleValue];

    CLLocationCoordinate2D coord1 = CLLocationCoordinate2DMake(coord1.latitude = Doblat1, coord1.longitude = Doblat1);

    NSLog(@" Coordinat ==== : %f -- %f",coord1.latitude,coord1.longitude) ;

    CLLocation *currLoc = [[CLLocation alloc] initWithLatitude:appDel.curr_lat longitude:appDel.curr_long];

    CLLocation *destLoc1 = [[CLLocation alloc] initWithLatitude:Doblat1 longitude:Doblong1];

    NSLog(@" Currennt Location : %@", currLoc);
    NSLog(@" Destination Location : %@" , destLoc1);

    distance = [destLoc1  distanceFromLocation:currLoc];

    NSLog(@" Distance  :   ------- %0.3f", distance);

    DistStr = [[NSString alloc]initWithFormat:@" %f",distance];

    [currLoc release];

    [destLoc1 release];
    [Arr_title retain];

    [tbl_nearplace reloadData];
} 
4

3 に答える 3

1

距離を保存する場合は、NSMutableArrayが必要です。

  1. NSMutableArray *distanceArray;クラス内スコープを宣言します。
  2. 初期化:distanceArray = [[NSMutableArray alloc] init];
  3. DistStr =[[NSString alloc]initWithFormat:@" %f",distance]; 次のコードを記述した後のforループで:

    [distanceArray addObject:DistStr];
    
于 2012-12-03T11:39:21.253 に答える
1

この配列に、繰り返し処理してそれらのアイテムを別の配列に追加したいものがあるとします。

NSArray *someArrayWithStuff = @[@"Hello",
                                @"Its",
                                @"Very",
                                @"Cold",
                                @"Outside",
                                @"Today"];

someArrayWithStuffのコンテンツをこの他の配列に追加して、NSMutableArrayを作成するとします。

NSMutableArray *theNewArrayWithOurStuff = [NSMutableArray array];

someArrayWithStuffをループします

for (int i = 0; i < someArrayWithStuff.count; i++) {
    // Add object to the new array
    [theNewArrayWithOurStuff addObject:[someArrayWithStuff objectAtIndex:i]];
}
于 2012-12-03T23:10:18.767 に答える
0
NSMutableArray *Distance=[[NSMutableArray alloc]Init];

for (i = 0; i < [Arr_Lat count]; i++)  
{
    NSString *latst1 = [[NSString alloc]initWithFormat:[Arr_Lat objectAtIndex:i]];
    NSString *longst1 = [[NSString alloc]initWithFormat:[Arr_Long objectAtIndex:i]];

    NSLog(@"First lat : %@",latst1);
    NSLog(@"First  ong : %@",longst1);

    double Doblat1 = [latst1 doubleValue];
    double Doblong1 = [longst1 doubleValue];

    CLLocationCoordinate2D coord1 = CLLocationCoordinate2DMake(coord1.latitude = Doblat1, coord1.longitude = Doblat1);

    NSLog(@" Coordinat ==== : %f -- %f",coord1.latitude,coord1.longitude) ;

    CLLocation *currLoc = [[CLLocation alloc] initWithLatitude:appDel.curr_lat longitude:appDel.curr_long];

    CLLocation *destLoc1 = [[CLLocation alloc] initWithLatitude:Doblat1 longitude:Doblong1];

    NSLog(@" Currennt Location : %@", currLoc);
    NSLog(@" Destination Location : %@" , destLoc1);

    distance = [destLoc1  distanceFromLocation:currLoc];

    NSLog(@" Distance  :   ------- %0.3f", distance);

    NSString *DistStr = [[NSString alloc]initWithFormat:@" %f",distance];

    [Distance addObject:DistStr];

    [DistStr release];
    [currLoc release];

    [destLoc1 release];
    [Arr_title retain];

    [tbl_nearplace reloadData];
} 
于 2012-12-03T11:49:01.260 に答える