2012-03-27 40 views
1

由於某種原因,我的自定義MKAnnotation類有一些問題,並將它添加到地圖中。我也試圖使用基本的MKPointAnnotation來查看這是否可行,但事實並非如此。在我的函數中,創建Annotation,然後添加它,同時打印NSLogs以驗證數據是否在註釋中。當它打印映射的註釋數組的內容時,計數爲0.有人知道爲什麼它沒有被添加到mapView嗎?iPhone - 添加自定義MKAnnotation到MapView不會被添加

- (void)viewDidLoad { 
    [map setMapType:MKMapTypeStandard]; 
    [map setDelegate:self]; 
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(30.451667, -84.268533), 16090.344, 16090.344); 
    viewRegion = [map regionThatFits:viewRegion]; 
    [map setRegion:viewRegion animated:YES]; 
    [map setShowsUserLocation:YES]; 
} 

- (void)loadBuilding { 
     if (buildAnnotation != nil) { 
      [map removeAnnotation:buildAnnotation]; 
     } 
     buildAnnotation = [[BuildingAnnotation alloc] initWithCoordinate:building.getLocation withName:building.getName withAddress:building.getAddress]; 
     [map addAnnotation:buildAnnotation]; 
     [map setCenterCoordinate:buildAnnotation.coordinate animated:YES]; 
     NSLog(@"%@\n%@\n %f, %f", buildAnnotation.title, buildAnnotation.subtitle, buildAnnotation.coordinate.latitude, buildAnnotation.coordinate.longitude); 
     NSLog(@"%i", [[map annotations] count]); 
    } 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    NSLog(@"IN DELEGATE"); 
    static NSString *identifier = @"MyLocation"; 
    if ([annotation isKindOfClass:[BuildingAnnotation class]]) { 

     MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier:identifier]; 
     if (annotationView == nil) { 
      annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
     } else { 
      annotationView.annotation = annotation; 
     } 

     annotationView.enabled = YES; 
     annotationView.canShowCallout = YES; 
     annotationView.pinColor = MKPinAnnotationColorGreen;   
     return annotationView; 
    } 

    return nil; 
} 

BuildingAnnotation.h:

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 

@interface BuildingAnnotation : NSObject <MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
} 

@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle; 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

-(id)initWithCoordinate:(CLLocationCoordinate2D) c; 
-(id)initWithCoordinate:(CLLocationCoordinate2D)c withName:(NSString*)name withAddress:(NSString*)address; 
- (CLLocationCoordinate2D) coordinate; 

@end 

BuildingAnnotation.m:

#import "BuildingAnnotation.h" 

@implementation BuildingAnnotation 

@synthesize coordinate, title, subtitle; 

- (id)initWithCoordinate:(CLLocationCoordinate2D) c { 
    coordinate = c; 
    title = @"Title"; 
    subtitle = @"Subtitle"; 
    return self; 
} 

-(id)initWithCoordinate:(CLLocationCoordinate2D)c withName:(NSString*)name withAddress:(NSString*)address { 
    self = [super init]; 
    if (self) { 
     coordinate = c; 
     title = name; 
     subtitle = address; 
    } 
    return self; 
} 

- (CLLocationCoordinate2D) coordinate { 
    return coordinate; 
} 


@end 
+1

有點明顯的問題,但是當你調用loadBuilding的時候map是否存在?另外,NSLog很好,但gdb可能會幫助你深入挖掘對象結構。 – 2012-03-27 07:44:59

+0

是的,地圖存在,我有一個地圖顯示用戶的位置和「IN DELEGATE」的NSLog確實被調用。我很抱歉,但在我2年的iOS編程中,我從來沒有真正使用GDB進行調試,哈哈,你能給我一個可能有用的例子嗎? – 2012-03-27 18:44:25

+1

@Muller - 你真的應該學習如何使用GDB(或者,現在LLDB!)。這裏有一些關於它的內容 - http://www.raywenderlich.com/10505/my-app-crashed-now-what-part-2 - 但是,爲此,這很容易。只需在'loadBuilding'開始處設置一個斷點並檢查是否設置了'map'(將鼠標懸停在它上面並查看它是否不是「0x0」)。 – mattjgalloway 2012-03-27 21:49:20

回答

0

想通了這是什麼,所有的代碼是正確的,但我試圖修改數據從另一個類的視圖控制器,所以我在做[self.storyboard [email protected]""]但是這是創建另一個對象,並修改,而不是修改原始視圖控制器。謝謝你mattgalloway & Cyril!