1

我很难找到如何为我的 CLLocationCoordinate2D 名称变量使用字符串。在我的代码中,我得到了 90 个位置:

CLLocationCoordinate2D location1;
...
CLLocationCoordinate2D location90;

我想调用这个方法:

locationConverToImage = [myMapView convertCoordinate:location1 toPointToView:drawView];

在这样的 for 循环中:

for (int i=1; i<=90; i++) {
    NSString *newCoord = @"location";
    [newCoord stringByAppendingString:[NSString stringWithFormat:@"%d",i]];
    locationConverToImage = [myMapView convertCoordinate:newCoord toPointToView:drawView];
}

但问题是我不能使用字符串作为变量名。我能怎么做 ?

谢谢。

4

2 回答 2

1

您本可以使用数组,但对于您的问题,这是我的回答:

for (int i=1; i<=90; i++) {
        NSString *newCoord = [NSString stringWithFormat:@"location%d", i];
        CLLocationCoordinate2D coordinate = (CLLocationCoordinate2D)[self valueForKey:newCoord];
        locationConverToImage = [myMapView convertCoordinate:coordinate toPointToView:drawView];
    }
于 2013-04-29T08:52:20.970 回答
0

你可以做这样的事情使用array

使用对象创建数组,CLLocation因为您不能CLLocationCoordinate2D直接添加到Array. 请参阅下面的代码

NSMutableArray *locations=[[NSMutableArray alloc] init];

    for(int i=1;i<=90;i++){
       CLLocation *loc=[[CLLocation alloc] initWithLatitude:LAT longitude:LNG];
       [locations addObject:loc];
    }

然后

for(CLLocation *loc locations){
  locationConverToImage = [myMapView convertCoordinate:loc.coordinate toPointToView:drawView];

}
于 2013-04-29T08:56:08.223 回答