2012-02-15 12 views
0

어떤 이유로 아래 코드는지도보기에 특수 효과를 추가하지 않습니다. 내가 로그 아웃 할 때 배열을 거의 50 번 통과하지만 0을 반환합니다. Attraction 객체는 MKAnnotation 프로토콜을 구현합니다.지도보기에 MKAnnotations가 추가되지 않았습니다.

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

. 감사.

+0

자기 매력은 관광 명소와 동일합니까? 당신은 self.mv가 0이 아니라고 확신합니까? – sch

+0

예 self.attractions는 매력과 같았습니다. 나는 그것을 변경하여 효과가 없는지 확인했습니다. 그리고 그것을 알아 내려고 노력하고 있지만 self.mv가 0이 아닌 것처럼 보입니다. 이유는 모르겠습니다. 스토리 보드에 연결했기 때문에 왜 그렇게되지 않을지 알 수 없습니다. –

+0

나는 그것을 알아낼 수 있었다. 나는지도보기의 끌어 당김 데이터를 업데이트하는 프로토콜을 가지고 있었고 메서드에서 viewDidLoad 이전의 forwardGeoocoderFoundLocation 메서드를 호출하고있었습니다. viewDidLoad 메서드에 대한 호출을 넣고 이제는 잘 작동합니다. 감사! –

답변

0

self.mvnil 인 경우 확인한 다음 self.mv.annotations.count을 반환합니다. nil (또는 0 또는 이와 비슷한 값). "%i"으로 포맷하면 0으로 기록됩니다.

하지만 제출 한 코드에는 아무 것도 없으므로 코드의 다른 부분에서 이유를 찾아야합니다.

+0

고맙습니다.지도에 주석을 추가하는 메소드를 호출 할 당시보기가로드되지 않았으므로 불가능했습니다. 내가 뭘 잘못했는지 알았고 지금은 효과가있다! –

관련 문제