views:

459

answers:

2

I use the loop below to populate my MapView. However it always shows only one pin at a time, no matter how many iterations i do.

Individually declaring items doesn't seem to have an impact either.

I'm using the initial 3.0 SDK with xCode 3.1.3 on osx 10.5.8, The 3.1 SDK changelog didn't make any mention of any fixes to the MKMapKit framework, so i haven't felt the need to download the 2.5GB file.

    for(NSDictionary* dict in results ){
    NSLog(@"Made Annotation  %@ at N%f E%f", [dict valueForKey:@"location"],[dict valueForKey:@"latitude"],[dict valueForKey:@"longitude"] );
    NSLog(@"List of keys %@", dict);

    LTAnnotation* pin = [[LTAnnotation alloc] initWithTitle: [dict valueForKey:@"location"]
                                     latitude: [dict objectForKey:@"latitude"]
                                    longitude: [dict objectForKey:@"longitude"]
    ];

    [MapView addAnnotation: pin];

}

This is output from the first logging statement

Made Annotation  London at N51.3 E0.07000000000000001
Made Annotation  Amsterdam at N52.22 E4.53

And the second is structure of the dictionary

List of keys {
    id = 0;
    latitude = 51.3;
    location = London;
    longitude = 0.07000000000000001;
    time = "12:00-13:00";
}
List of keys {
    id = 1;
    latitude = 52.22;
    location = Amsterdam;
    longitude = 4.53;
    time = "12:00-13:00";
}

In case your interested here's my implementation of LTAnnotation

@interface LTAnnotation(Private)
    double longitude;
    double latitude;
@end

@implementation LTAnnotation

@synthesize title;
@synthesize subTitle;
-(id) initWithTitle:(NSString*)pTitle latitude:(NSNumber*)latDbl longitude:(NSNumber*) longDbl{
    self = [super init];

    self.title = pTitle;

    latitude = [latDbl doubleValue];
    longitude = [longDbl doubleValue];
    NSLog(@"Create Annotation for %@ at %fN %fE",pTitle,[latDbl doubleValue],[longDbl doubleValue]);
    return self;

}


-(CLLocationCoordinate2D) coordinate
{
    CLLocationCoordinate2D retVal;

    retVal.latitude = latitude;
    retVal.longitude = longitude;

    return retVal; 
}
@end

This all combines to produce this ...

alt text

Any ideas at where i'm going wrong? Thanks

A: 

Try setting the latitude and longitude as floats.

http://stackoverflow.com/questions/1383296/why-mkmapview-region-is-different-than-requested

nevan
Tried this to no avail, thanks anyway
Jonathan
+1  A: 

Two small things I noticed which might help solve the problem:

  • You are not releasing the pin in the first code sample which would create a leak
  • You are not checking if "self = [super init];" in your second code sample was successful ("if(self = [super init]){...} return self"). The NSLog also only outputs the parameters passed to the init method, not the instance methods of the object

And most importantly, I just noticed this in your init method:

latitude = [latDbl doubleValue];
longitude = [longDbl doubleValue];

You are not using Objective-C 2 style accessor methods (self.latitude = ...) and not retaining the autoreleased values. This probably means the variables are disappearing and that's why you cannot see the annotations, as they don't have valid coordinates.

Sascha Konietzke
Sloppy coding on my part, i believe you were correct on the 3rd point, changing the values to properties fixed it. Thanks
Jonathan