2013-09-26 55 views
1

我正在使用以下代碼將註釋添加到地圖視圖。問題是,註釋不會出現在iOS的7,但在iOS6的地圖註釋不會在iOS 7中顯示

在我viewDidLoad方法都顯示出來:

for (int i=0; i<[self.listingNodesArray count]; i++) { 
    MyAnnotation* annotation= [MyAnnotation new]; 

    CLLocationCoordinate2D coordinate; 
    coordinate.latitude = [[[[self.listingNodesArray objectAtIndex:i] objectForKey:@"address"] objectForKey:@"lat"] doubleValue]; 
    coordinate.longitude = [[[[self.listingNodesArray objectAtIndex:i] objectForKey:@"address"] objectForKey:@"lng"] doubleValue]; 

    annView.image = [UIImage imageNamed:@"PIN_img.png"];// sets image for pin 

    annotation.coordinate = coordinate; 

    annotation.title = [[self.listingNodesArray objectAtIndex:i] objectForKey:@"title"]; 
    annotation.subtitle = [[[self.listingNodesArray objectAtIndex:i] objectForKey:@"address"] objectForKey:@"address"]; 

    NSNumber *listingIdNumber = [[self.listingNodesArray objectAtIndex:i] objectForKey:@"id"]; 

    annotation.catListingMapId = listingIdNumber; 

    [self.topMapView addAnnotation: annotation]; 

} 

[self.view addSubview:self.topMapView]; 

viewForAnnotation

- (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation { 
    annView = nil; 
    if(annotation != mapingView.userLocation) 
    { 

     static NSString *defaultPinID = @""; 
     annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
     if (annView == nil) 
      annView = [[MKAnnotationView alloc] 
         initWithAnnotation:annotation reuseIdentifier:defaultPinID] ; 


     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
     // [rightButton addTarget:self 
     //    action:@selector(showDetails:) 
     // forControlEvents:UIControlEventTouchUpInside]; 
     annView.rightCalloutAccessoryView = rightButton; 

     annView.canShowCallout = YES; 

    } 

    return annView; 
} 

和我的註釋文件:

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

@interface MapViewAnnotation : NSObject <MKAnnotation> { 

    NSString *title; 
    CLLocationCoordinate2D coordinate; 

} 

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

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d; 

@end 

    #import "MapViewAnnotation.h" 

@implementation MapViewAnnotation 

@synthesize title, coordinate; 

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d { 

    title = ttl; 
    coordinate = c2d; 
    return self; 
} 


@end 
+2

請勿設置'annView.image' _outside_ viewForAnnotation方法。在設置附件視圖的同時設置委託方法。如果它在iOS 6中「有效」,那只是偶然。 – Anna

回答

2

正如AnnaKarenina指出的,您應該設置在viewForAnnotation方法中您的註釋視圖的。

這可能在iOS 6中有些「工作」,但肯定不完全。在將註釋添加到地圖之前(並且在創建此註釋的註釋視圖之前),您正試圖設置註釋視圖的image。充其量,您爲前面的註釋設置註釋視圖image,而不是您認爲自己的註釋視圖。

如果您的各種註釋需要獨特的圖像,請繼續並將imageName屬性添加到您的註釋中,然後您可以讓viewForAnnotation使用該屬性設置註釋視圖的圖像。這就是您應該爲註釋視圖創建UIImage的地方。坦率地說,我建議你刪除annView伊娃,以避免嘗試訪問viewForAnnotation以外的註釋視圖的誘惑。

順便說一句,在MapViewAnnotation類(簡稱別處MyAnnotation)有很多自己的(例如小問題,你init並沒有叫super,你定義的字符串是copy參數,但init沒有做(例如title = [ttl copy]),它現在建議您不要顯式創建支持您的屬性的ivars等),但這些與您的註釋視圖圖像有關的更廣泛的問題無關。