2011-03-11 65 views
0

嗨夥計們,讓我能夠顯示註釋(2)從源代碼中發現at this link(以下被註釋掉的代碼後內)。加載註釋地圖檢視從plist中不工作

但是,我從一個plist中加載註釋信息,正確裝入(通過調試確認)進入的MapView但由於某種原因註釋拒絕出示當我運行在模擬器中的應用。下面是相關代碼:

註釋頭:

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

@interface ArboretumAnnotation : NSObject <MKAnnotation> { 
    NSString *title; 
    NSString *subtitle; 
    UIImage *image; 

    CLLocationCoordinate2D coordinate; 

} 

@property (nonatomic, retain) NSString *title; 
@property (nonatomic, retain) NSString *subtitle; 
@property (nonatomic, retain) UIImage *image; 

@property (nonatomic) CLLocationCoordinate2D coordinate; 

@end 

註釋實現:

#import "ArboretumAnnotation.h" 


@implementation ArboretumAnnotation 

@synthesize title; 
@synthesize subtitle; 
@synthesize image; 
@synthesize coordinate; 

-(id)init{ 
    self = [super init]; 
    if(!self) 
     return nil; 

    self.title = nil; 
    self.subtitle = nil; 
    self.image = nil; 

    return self; 
} 

-(id)initWithCoordinate:(CLLocationCoordinate2D)inCoord{ 
    self = [self init]; 
    self.coordinate = inCoord; 
    return self; 
} 
@end 

的MapViewController頭:

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


@interface MapViewController : UIViewController <MKMapViewDelegate> { 
    MKMapView *mapView; 
} 

@property (nonatomic, retain) IBOutlet MKMapView *mapView; 

- (void)showCurrentLocationButtonTapped:(id)sender; 
- (void)loadAnnotations; //helper function that loads all overlay data from plist 
- (void)loadOverlays; //helper function that loads all overlay data from plist 
- (void)handleGesture:(UILongPressGestureRecognizer *)gestureRecognizer; 

@end 

的MapViewController實現:

#import "MapViewController.h" 
#import "MKMapView+ZoomLevel.h" 
#import "ArboretumRegionOverlay.h" 
#import "ArboretumAnnotation.h" 
#import "ArboretumAnnotationView.h" 
#import "LocationDetailViewController.h" 


#define UCD_LATITUDE 38.531728 
#define UCD_LONGITUDE -121.755327 

@implementation MapViewController 

@synthesize mapView; 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    //mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; 
    mapView.delegate = self; 

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Location Info" style:UIBarButtonItemStylePlain target:self action:nil]; 
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100 target:self action:@selector(showCurrentLocationButtonTapped:)]; 


    [self loadAnnotations]; 

    [self loadOverlays]; 
    [mapView setNeedsDisplay]; 

    /* 
    //TEST LOAD AN ANNOTATION 
    //MKPointAnnotation *testAnnot = [[MKPointAnnotation alloc] init]; 
    ArboretumAnnotation *arboAnnot = [[ArboretumAnnotation alloc] init]; 
    CLLocationCoordinate2D workingCoord; 
    workingCoord.latitude = 38.529977; 
    workingCoord.longitude = -121.76; 
    [arboAnnot setCoordinate:workingCoord]; 
    [arboAnnot setTitle:@"Test Title"]; 
    [arboAnnot setSubtitle:@"Test Subtitle"]; 
    [mapView addAnnotation:arboAnnot]; 
    [arboAnnot release]; 

    ArboretumAnnotation *arboAnnot2 = [[ArboretumAnnotation alloc] init]; 
    CLLocationCoordinate2D workingCoord2; 
    workingCoord2.latitude = 38.531594; 
    workingCoord2.longitude = -121.762129; 
    [arboAnnot2 setCoordinate:workingCoord2]; 
    [arboAnnot2 setTitle:@"Test A really really really really long title Title2"]; 
    [arboAnnot2 setSubtitle:@"Test A really really really really long sub Subtitle2"]; 
    [mapView addAnnotation:arboAnnot2]; 
    [arboAnnot2 release]; 
    */ 

    //detects press gestures used for producing region overlay callout 
    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
              initWithTarget:self action:@selector(handleGesture:)]; 
    lpgr.minimumPressDuration = 0.3; //user must press for 1 second 
    //[mapView addGestureRecognizer:lpgr]; 
    [lpgr release]; 


    //initialize the map view (location and zoom) 
    CLLocationCoordinate2D centerCoord = { UCD_LATITUDE, UCD_LONGITUDE }; 
    [mapView setCenterCoordinate:centerCoord zoomLevel:13 animated:NO]; 
} 

- (void)showCurrentLocationButtonTapped:(id)sender { 
    NSLog(@"Showing current location."); 

    if ([mapView showsUserLocation] == NO) { 
     [mapView setShowsUserLocation:YES]; 
    } 
    [mapView setCenterCoordinate:mapView.centerCoordinate zoomLevel:13 animated:YES]; 

} 

- (void)loadAnnotations{ 
    //retrieve path of plist file and populate relevant types with its information 
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"ArboretumGardens" ofType:@"plist"]; 
    NSDictionary *rootOfArboDataPlistDict = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; 
    NSMutableArray *arboAnnotations = [[NSMutableArray alloc] init]; 

    for (NSString *key in rootOfArboDataPlistDict) { 

     NSMutableDictionary *arboDict = [rootOfArboDataPlistDict objectForKey:key]; 

     //array containing annotation information: latitude, longitude, title, subtitle(see PermitData.plist) 
     NSArray *annotationsArray = [arboDict objectForKey:@"annotations"]; 

     CLLocationCoordinate2D workingCoordinate; 
     //loop through annotations array, creating parking annotations filled with the information found in the plist 
     for(NSDictionary *annotationContainerDict in annotationsArray){ 

      ArboretumAnnotation *arboAnnot = [[ArboretumAnnotation alloc] init]; 
      workingCoordinate.latitude = [[annotationContainerDict objectForKey:@"latitude"] doubleValue]; 
      workingCoordinate.longitude = [[annotationContainerDict objectForKey:@"longitude"] doubleValue]; 
      [arboAnnot setCoordinate:workingCoordinate]; 
      [arboAnnot setTitle:[annotationContainerDict objectForKey:@"title"]]; 
      [arboAnnot setSubtitle:[annotationContainerDict objectForKey:@"subtitle"]]; 
      [arboAnnotations addObject:arboAnnot]; 
      [arboAnnot release]; 
     }//for 
    }//for 
    [mapView addAnnotations:arboAnnotations]; 
    [arboAnnotations release]; 
} 

//... 

- (ArboretumAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    if ([annotation isKindOfClass:MKUserLocation.class]) { 
     //it's the built-in user location annotation(blue dot) 
     return nil; 
    } 

    NSString *annotIdentifier = @"annotation"; 

    MKPinAnnotationView *recycledAnnotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:annotIdentifier]; 
    if (!recycledAnnotationView) { 
     MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] 
               initWithAnnotation:annotation reuseIdentifier:annotIdentifier] autorelease]; 

     UIImage *iconImage = [UIImage imageNamed:@"arboretum.png"]; 

     CGRect resizeRect; 

     resizeRect.size = iconImage.size; 
     CGSize maxSize = CGRectInset(self.view.bounds, 
            10.0f, 
            10.0f).size; 
     maxSize.height -= self.navigationController.navigationBar.frame.size.height + 40.0f; 
     if (resizeRect.size.width > maxSize.width) 
      resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height/resizeRect.size.width * maxSize.width); 
     if (resizeRect.size.height > maxSize.height) 
      resizeRect.size = CGSizeMake(resizeRect.size.width/resizeRect.size.height * maxSize.height, maxSize.height); 

     customPinView.image = iconImage; 
     //customPinView.image.frame = CGRectMake(kBorder, kBorder, kWidth - 2 * kBorder, kWidth - 2 * kBorder); 
     customPinView.opaque = NO; 
     //customPinView.pinColor = MKPinAnnotationColorPurple; 
     customPinView.canShowCallout = YES; 

     //display a disclosure icon on the right side of each annotation's callout 
     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     customPinView.rightCalloutAccessoryView = rightButton; 

     return customPinView; 
    } else { 
     recycledAnnotationView.annotation = annotation; 
    } 

    return recycledAnnotationView; 

} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


@end 

提前感謝!

+0

,你試過沒有使用MKPinAnnotationView和使用MKAnnotationView呢? – 2011-03-12 09:22:46

+0

其實我發現了這個問題,我只需要開始發佈它(很快,我保證)。提示:上面的代碼工作非常好。 – Stunner 2011-03-12 22:49:51

回答

2

原來,問題是我是如何獲取的座標數據。我無意間將緯度與緯度進行了交換,並且這些點不會顯示在全球地圖上的任何點上。編譯器沒有發出任何警告或錯誤,所以它沒有被注意到......所以,對我來說非常尷尬,但這是你如何學習的,對吧?

0

我不知道如果是這樣的問題,但

self = [self init]; 

假設是

self = [super init]; 
+0

其實沒有,因爲我們需要確保類的已經被初始化,有什麼更好的辦法做到這一點,而不是調用從initWithCoordinate方法開始常規初始化函數?另外,在任何情況下,init方法都會發送[super init]消息,所以我們不必擔心這一點。但很好的捕捉,否則。 – Stunner 2011-03-11 04:38:59