1
votes

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 below, 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..

2
You seem to have setLongitude and setLatitude as class methods. What do they do? How is you init method defined?Deepak Danduprolu
I didn't have the init method defined and actually now that I looked at my custom annotation class I didn't have the coordinate property in the .h so I'm confused as to why it even worked with even one annotation. I'm going to look further into properly implementing a custom annotation class because maybe that is my problem.user567677
-(void)setLongitude:(double)lon { Clongitude = lon; }user567677
And what is Clongitude? Is it a static variable and how to you implement the longitude instance method?Deepak Danduprolu
Clongtitude is a double value and I'm not sure which longitude instance method you're asking about. I thought that all that was required is the @property (nonatomic, readonly) CLLocationCoordinate2D coordinate; and also I implemented this function to set the coordinate - (CLLocationCoordinate2D)coordinate; { CLLocationCoordinate2D theCoordinate; theCoordinate.latitude = Clatitude; theCoordinate.longitude = Clongitude; return theCoordinate; }user567677

2 Answers

0
votes

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.

0
votes

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];