I can't add more than 1 annotation to MKMapview :|
So I'm pulling my hair out over this one. I've been tracking down a bug in my app. Originally, I was trying to load long / lat coordinates from a database and do a for loop, adding each annotation to the map view. This seemed simple enough, but for some reason when I tried to add the annotations in the loop it would only show 1 annotation in the end.
So, using the code be开发者_开发问答low, I decided to try and simply add two annotations to MKMapView to be sure that I can do this in the first place, and it doesn't work!
[NewAnnotation setLongitude:-104.6200448];
[NewAnnotation setLongitude:50.4908343];
NewAnnotation *newAnnotation = [[NewAnnotation alloc] init];
[self.mapAnnotations insertObject:newAnnotation atIndex:0];
[newAnnotation release];
[CelebsAnnotation setLongitude:-90.6200448];
[CelebsAnnotation setLatitude:51.4908343];
CelebsAnnotation *celebsAnnotation = [[CelebsAnnotation alloc] init];
[self.mapAnnotations insertObject:celebsAnnotation atIndex:1];
[celebsAnnotation release];
[self.mapView addAnnotations:mapAnnotations];
The annotations show up in their correct locations if I only add one of them to the mapAnnotations array (I have to adjust the index to 0 if I only add the CelebsAnnotation), but when I try to add both, they show up in the same location on the map!? Any ideas as to why this would happen? I am so confused and frustrated..
Your problem is here,
- (CLLocationCoordinate2D)coordinate; {
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = Clatitude;
theCoordinate.longitude = Clongitude;
return theCoordinate;
}
You are creating a new CLLocationCoordinate2D
every time coordinate
is requested an you are setting this to the static variables Clongitude & Clatitude
. Since these are static variables, you will be returning similar CLLocationCoordinate2D
objects for all instances of CelebsAnnotation
. So basically all instances will have the same coordinate.
You should rather have an init..
method that takes in a CLLocationCoordinate2D
object and set it to an instance variable. For example, look at this tutorial
.
You will end up doing something like this,
CLLocationCoordinate2D coordinate;
coordinate.longitude = -90.6200448;
coordinate.latitude = 51.4908343;
CelebsAnnotation *celebsAnnotation = [[CelebsAnnotation alloc] initWithCoordinate:coordinate];
[self.mapAnnotations insertObject:celebsAnnotation atIndex:1];
[celebsAnnotation release];
Here, this code fetching value of lattitude and longitude from database and, placing annotation in map:
while (sqlite3_step(statement)==SQLITE_ROW)
{
char *iTtl=(char *) sqlite3_column_text(statement, 1);
trackedImageTitle=[NSString stringWithUTF8String:iTtl];
char *iLat=(char *) sqlite3_column_text(statement, 2);
trackedLocationLattitude=[NSString stringWithUTF8String:iLat];
//NSLog(@"lat=%@",trackedLocationLattitude);
char *iLon=(char *) sqlite3_column_text(statement, 3);
trackedLocationLongitude=[NSString stringWithUTF8String:iLon];
//NSLog(@"lon=%@",trackedLocationLongitude);
char *lcn=(char *) sqlite3_column_text(statement, 4);
trackedLocation=[NSString stringWithUTF8String:lcn];
int iId=sqlite3_column_int(statement, 0);
NSString *iIdS =[NSString stringWithFormat:@"%d", iId];
[allImageIds addObject:[NSString stringWithFormat:@"%@",iIdS]];
//theImage=iId;
//NSLog(@"the image=%d, iId= %d",theImage,iId);
location.latitude=[trackedLocationLattitude floatValue];
location.longitude=[trackedLocationLongitude floatValue];
addAnnotation = [[MapViewAnnotation alloc] initWithLocation:location withTitle:[NSString stringWithFormat:@"Tuscany"] withSubTitle:[NSString stringWithFormat:@"Italy"] withImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",iId]]];
addAnnotation.mTitle=[NSString stringWithFormat:@"%@",trackedImageTitle];
addAnnotation.mSubTitle=[NSString stringWithFormat:@"%@",trackedLocation];
////NSLog(@"%@",addAnnotation.mTitle);
[mapView addAnnotation:addAnnotation];
}
mapView
is instance of MKMapView
.
精彩评论