-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. 在for循环之后DistStr =[[NSString alloc]initWithFormat:@" %f",distance]; 编写如下代码:

    [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 回答