iphone - I can't add more than 1 annotation to MKMapview :| -
so i'm pulling hair out on one. i've been tracking down bug in app. originally, trying load long / lat coordinates database , loop, adding each annotation map view. seemed simple enough, reason when tried add annotations in loop show 1 annotation in end.
so, using code below, decided try , add 2 annotations mkmapview sure can in first place, , 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 in correct locations if add 1 of them mapannotations array (i have adjust index 0 if add celebsannotation), when try add both, show in same location on map!? ideas why happen? confused , frustrated..
your problem here,
- (cllocationcoordinate2d)coordinate; { cllocationcoordinate2d thecoordinate; thecoordinate.latitude = clatitude; thecoordinate.longitude = clongitude; return thecoordinate; } you creating new cllocationcoordinate2d every time coordinate requested setting static variables clongitude & clatitude. since these static variables, returning similar cllocationcoordinate2d objects instances of celebsannotation. instances have same coordinate.
you should rather have init.. method takes in cllocationcoordinate2d object , set instance variable. example, @ this tutorial.
you end doing 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];
Comments
Post a Comment