2

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.

Conrad Shultz
  • 8,748
  • 2
  • 31
  • 33
  • What is `myAnnotation1`? Are you expecting your `annotations` array to contain all the annotation objects you are adding to the map? –  May 14 '12 at 17:08
  • sorry put wrong line in code but has been edited now. I have 6 obejects in my array but could be anywhere from 0 to 10. I am trying to get an annotation for each object with out having to hard code each objectatindex – charlie johnston May 14 '12 at 17:15
  • P.S.- The line 'myAnnotation5.title=[NSString stringWithFormat:@"%@"entity.EntityNo]' shouldn't even compile without a comma (added in my answer). Please make sure that when asking a question you copy and paste your EXACT code. – Conrad Shultz May 14 '12 at 17:26

2 Answers2

3

You only have one myAnnotation5 object. When you set its coordinate, title, etc., you are setting it for that instance, which you happen to have added to annotations multiple times. Hence every entry in annotations will have the last set of properties you set - since every entry in annotations is actually the same object.

To remedy this, you need to create your myAnnotation5 object anew each iteration of the loop, i.e.

for (int i = 0; i < _getDBInfo.count; i++) {
    MyAnnotation* myAnnotation5=[[MyAnnotation alloc] init];
    ...
    myAnnotation5.coordinate=theCoordinate5;
    myAnnotation5.title=[NSString stringWithFormat:@"%@", entity.EntityNo];
    myAnnotation5.subtitle=[NSString stringWithFormat:@"%@", entity.EntityName];
    ...
    [mapView addAnnotation:myAnnotation5];
}

Two asides:

  1. I hope you are building with ARC, otherwise you are leaking memory left and right.
  2. Since MKMapView has an -annotations property, there is likely little reason for you to keep your own annotations array - just keep a reference to mapView.
Conrad Shultz
  • 8,748
  • 2
  • 31
  • 33
1

Move this line:

MyAnnotation* myAnnotation5=[[MyAnnotation alloc] init];

to inside the for-loop just before setting the properties on myAnnotation5.

The way it is right now, you are creating only one MyAnnotation object and modifying its properties repeatedly.

  • Please have a look at this question as well : http://stackoverflow.com/questions/30396754/ios-custom-annotation-a-view-below-the-annotation-pin – iYoung May 23 '15 at 06:04
  • @Anna please help me on this http://stackoverflow.com/questions/36330331/route-is-not-displaying-in-map – Bhavin Ramani Mar 31 '16 at 10:23