user1104325
user1104325

Reputation: 161

Select specific annotation

I'm looping through SQLite results and add annotations to a map

 while(sqlite3_step(statement) == SQLITE_ROW) {

     double latitude = sqlite3_column_double(statement, 0);
     double longitude = sqlite3_column_double(statement, 1);

     MKPointAnnotation *point = [[MKPointAnnotation alloc] init];

     CLLocationCoordinate2D location;
     location.latitude = latitude;
     location.longitude = longitude;
     [point setCoordinate:(location)];
     [point setTitle:venuename];
     [point setSubtitle:@"hello"];

     [mapView addAnnotation:point];

     [point release];

 } 

Works great. Now, lets say I want to Select the 3nd Annotation, I do

id<MKAnnotation> myAnnotation = [mapView.annotations objectAtIndex:2];    
[mapView selectAnnotation:myAnnotation animated:NO];

However, it seems annotations are added to the map in random order, and not according to the order of my loop. So the "objectAtIndex:2" is always a different annotation, and not the second Database result.

Is there a way how I can give an annotation some unique ID before I add them to the map, which I can then use to select them ?

Upvotes: 1

Views: 2064

Answers (1)

ale84
ale84

Reputation: 1406

An annotation can be any custom object that you want, provide it conforms to the MKAnnotation Protocol (see here)

So if you need your annotations to have an ID, you can declare a property for it in the code for the custom annotation object.

Upvotes: 3

Related Questions