For some reason the code below is not adding annotations to the map view. When I log out a count it returns 0, although it goes through the array almost 50 times. The Attraction object implements the MKAnnotation protocol.
-(void)forwardGeocoderFoundLocation
{
int searchResults = [attractions count];
for(int i = 0; i < searchResults; i++){
Attraction *a = [[Attraction alloc] init];
a.address = [(Attraction *)[attractions objectAtIndex:i] address];
a.city = [(Attraction *)[attractions objectAtIndex:i] city];
a.state = [(Attraction *)[attractions objectAtIndex:i] state];
NSString *address = [NSString stringWithFormat:@"%@,%@,%@", a.address, a.city, a.state];
NSString *theAddress = [address stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", theAddress];
NSString *locationString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString] usedEncoding:nil error:nil];
//NSLog(@"%@", locationString);
[[attractions objectAtIndex:i] setLocation:[[CLLocation alloc] initWithLatitude:43.1330340 longitude:-77.6376329]];
NSLog(@"Attraction location = %@", [[attractions objectAtIndex:i] location]);
[self.mv addAnnotation:[self.attractions objectAtIndex:i]];
NSLog(@"Mapview has %i annotations.", self.mv.annotations.count);
}
}
Here’s the Attraction.h file:
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface Attraction : NSObject<MKAnnotation>
@property(nonatomic,assign)int attractionID;
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *address;
@property(nonatomic,copy)NSString *description;
@property(nonatomic,copy)NSString *city;
@property(nonatomic,copy)NSString *state;
@property(nonatomic,copy)CLLocation *location;
+ (id)attraction;
- (id)initWithID:(int)a_id name:(NSString *)a_name address:(NSString *)a_address description:(NSString*)a_description city:(NSString*)a_city state:(NSString *)a_state;
@end
And here’s the Attraction.m file:
#import "Attraction.h"
@implementation Attraction
@synthesize attractionID, name, address, description, city, state, location;
- (CLLocationCoordinate2D)coordinate{
return self.location.coordinate;
}
- (NSString *)title{
return self.name;
}
- (NSString *)subtitle{
return self.name;
}
#pragma mark -
+ (id)attraction {
return [[Attraction alloc] init ];
}
- (id)initWithID:(int)a_id name:(NSString *)a_name address:(NSString *)a_address description:(NSString*)a_description city:(NSString*)a_city state:(NSString *)a_state {
self = [super init];
self.attractionID = a_id;
self.address = a_address;
self.name = a_name;
self.description = a_description;
self.city = a_city;
self.state = a_state;
return self;
}
- (id)init
{
return [self initWithID:0 name:@"TBD" address:@"TBD" description:@"TBD" city:@"TBD" state:@"TBD"];
}
@end
Any help would be appreciated. Thanks.
if
self.mvisnil, as you confirmed, thenself.mv.annotations.countreturnnil(or 0, or something like that). And that is logged as 0 when you format it as"%i".But there is nothing in the code you submitted can cause it to be nil, so you must look for the reason in other parts of your code.