I have a mutablearray that is populated from sqlite db in ios. I have gotten the annotations to load and view properly. My question is how can I write a loop that will add annotations with the size of the array. I have tried the following code and get and display the last entry in the array
NSMutableArray *annotations=[[NSMutableArray alloc] init];
CLLocationCoordinate2D theCoordinate5;
MyAnnotation* myAnnotation5=[[MyAnnotation alloc] init];
for (int i = 0; i < _getDBInfo.count; i++) {
dbInfo *entity = [_getDBInfo objectAtIndex:i];
NSNumber *numlat=[[NSNumber alloc] initWithDouble:[entity.Latitude doubleValue]];
NSNumber *numlon=[[NSNumber alloc] initWithDouble:[entity.Longitude doubleValue]];
NSLog(@"%d",[_getDBInfo count]);
la=[numlat doubleValue];
lo=[numlon doubleValue];
theCoordinate5.latitude=la;
theCoordinate5.longitude=lo;
myAnnotation5.coordinate=theCoordinate5;
myAnnotation5.title=[NSString stringWithFormat:@"%@"entity.EntityNo];
myAnnotation5.subtitle=[NSString stringWithFormat:@"%@",entity.EntityName];
[mapView addAnnotation:myAnnotation5];
[annotations addObject:myAnnotation5];
}
I guess my question is how can I create and add to my view annotation objects based on the count in my array?
Any help is much appreciated.
I am new to iOS as well as programming so please be gentle.
You only have one
myAnnotation5object. When you set itscoordinate,title, etc., you are setting it for that instance, which you happen to have added toannotationsmultiple times. Hence every entry inannotationswill have the last set of properties you set – since every entry inannotationsis actually the same object.To remedy this, you need to create your
myAnnotation5object anew each iteration of the loop, i.e.Two asides:
MKMapViewhas an-annotationsproperty, there is likely little reason for you to keep your ownannotationsarray – just keep a reference tomapView.