Reputation: 864
Attempting to take a NSSDictionary that contains all my locations, titles, and subtitles and enumerate through them and add them to my mapView.The first part seems fine; I read the Plist and can access the various parts. What I'm having problems with is running through the enumeration.
The NSLog asking for the title correctly reports that property to the console.
In the end,though, I see no pins. In addition, after the NSLog call:
NSLog(@"My %i annotations are: %@", self.mapView.annotations.count, self.mapView.annotations);
I get in response:
"My 1 annotations are: (
"<MKPointAnnotation: 0xd0151f0>" )
Which appears to be just one memory location for one annotation. Sigh.
I'm sure I'm doing something simple and wrong, but would appreciate any help. I've been searching all day, and seen some things that appear to be answers here, but not anything that helps me in this particular construction.
Thanks you in advance.
The following code is fired off in the main viewController, the first method seen is called at the end of ViewDidLoad.
-(void) loadUpLocations{
NSPropertyListFormat format;
NSString *errorDesc=nil;
//make path to plist in bundle
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"locations2" ofType:@"plist"];
//fill data object with contents of file
NSData *myData = [[NSData alloc] initWithContentsOfFile:plistPath];
myLocations=[NSPropertyListSerialization propertyListFromData:myData
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
if (!myLocations) {
NSLog(@"Error reading Plist: %@ format: %@", errorDesc,format);
}
myAnnotation=[[MKPointAnnotation alloc]init];
for (NSString *loc in myLocations) {
NSDictionary *value =[myLocations objectForKey:loc];
//NSLog(@"Loc is equal to: %@",loc);
//loop through and make annotations
myAnnotation.coordinate = CLLocationCoordinate2DMake(
[[value objectForKey:@"latitude"] doubleValue],
[[value objectForKey:@"longitude"] doubleValue]);
myAnnotation.title = [value objectForKey:@"title"];
myAnnotation.subtitle = [value objectForKey:@"subtitle"];
NSLog(@"%@",myAnnotation.title);
[self.mapView addAnnotation:myAnnotation];
}
NSLog(@"My %i annotations are: %@", self.mapView.annotations.count, self.mapView.annotations);
}
- (IBAction)zoomToLocation:(id)sender {
//set center for zoom
MKCoordinateRegion region;
region.center=CLLocationCoordinate2DMake(33.75070416856952, -84.37034368515015);;
//set level of zoom
MKCoordinateSpan span;
span.latitudeDelta=.03;
span.longitudeDelta=.03;
region.span=span;
[mapView setRegion:region animated:YES];
}
// This gets called every time an annotation is in the map view
- (MKAnnotationView *)mapView:mapView viewForAnnotation:annotation{
NSLog(@"in annotationsView");
MKPinAnnotationView *myPins=[[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:@"myPins"];
myPins.animatesDrop=YES;
return myPins;
}
Upvotes: 2
Views: 1782
Reputation:
This line:
myAnnotation=[[MKPointAnnotation alloc]init];
is currently outside the for
loop that creates the annotations so only one object ever gets instantiated and you keep modifying that one instance.
Move that line to inside the for
loop (eg. above the line that sets the coordinate).
Also, if you are not using ARC, add a [myAnnotation release];
after the addAnnotation
line.
Upvotes: 2