MKAnnotations未添加到mapview

时间:2012-02-15 21:42:26

标签: objective-c ios

由于某种原因,下面的代码不会在地图视图中添加注释。当我注销一个计数时,它返回0,虽然它通过阵列几乎50次。 Attraction对象实现MKAnnotation协议。

-(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);

}

}

这是Attraction.h文件:

#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

这是Attraction.m文件:

#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

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

如果self.mvnil,则在您确认后,self.mv.annotations.count会返回nil(或0,或类似内容)。当您将其格式化为"%i"时,它会记录为0。

但是您提交的代码中没有任何内容可以导致它为零,因此您必须在代码的其他部分查找原因。