1

I got some trouble to find how to use a string for my CLLocationCoordinate2D name variable. In my code I got 90 location :

CLLocationCoordinate2D location1;
...
CLLocationCoordinate2D location90;

And I would like to call this method :

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

in a for loop like this :

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

But the problem is that I can't use string for the variable name. How can I do ?

Thanks.

Glou94
  • 37
  • 8

2 Answers2

1

you could have used arrays, but for your question this is my answer:

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];
    }
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
0

You can do something like this using array

Create your array with CLLocation objects since you can't add CLLocationCoordinate2D directly to an Array. See the code below

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

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

Then

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

}
iphonic
  • 12,615
  • 7
  • 60
  • 107
  • How can I use LAT and LNG in the first loop ? I tried NSArray *longt = @[longitude1,...,longitude90]; But not working, because it's not an object. :/ – Glou94 Apr 29 '13 at 09:49
  • You must be getting `LAT` and `LNG` from somewhere ? And I have mentioned that `CLLocationCoordinate2D ` are not object use `CLLocation` instead.. – iphonic Apr 29 '13 at 10:03
  • you cannot save non-object instances in any objc container class (like NSArray). To save CLLocation in NSArray, you should use CLLocationCoordinate2D loc = kCLLocationCoordinate2DInvalid; NSValue *val = [NSValue valueWithBytes:&loc objCType:@encode(CLLocation)]; NSArray *array = @[ val ]; CLLocationCoordinate2D locExtracted; [val getValue:&locExtracted]; – art-divin Apr 29 '13 at 10:05
  • I wrote them in the code like this : CLLocationCoordinate2D location1; location1.latitude=2.399483; location1.longitude=48.800983; – Glou94 Apr 29 '13 at 10:12