2012-11-08 64 views
1

該位置正在工作,但標題不顯示,最奇怪。MapKit引腳標題不顯示

location.latitude = (double) 51.501468; 
location.longitude = (double) -0.141596; 

// Add the annotation to our map view 
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"ABC" andCoordinate:location]; 
[self.mapView addAnnotation:newAnnotation]; 
// [newAnnotation release]; 

MapViewAnnotation.h

#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 

和MapViewAnnotation.m

#import "MapViewAnnotation.h" 

@implementation MapViewAnnotation 

@synthesize title, coordinate; 

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d { 
title = ttl; 
coordinate = c2d; 
return self; 
} 

@end 

[newAnnotation發行]被remmed守到ARC快樂:-) 任何想法?

回答

0

該代碼看起來不錯(除了非標準執行init方法)。

標題(標註)沒有出現,最有可能的原因是,在您viewForAnnotation委託方法,你是不是設置canShowCalloutYES(默認爲NO)。

viewForAnnotation委託方法中,在創建MKAnnotationView後,將canShowCallout設置爲YES


無關的標題/標註不顯示在您的MapViewAnnotationinit方法應該實現這樣的:

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d { 
    self = [super init]; 
    if (self) { 
     title = ttl; 
     coordinate = c2d; 
    } 
    return self; 
} 
1

這奏效了:

[mapView selectAnnotation:newAnnotation animated:YES]; 

以前標題只會在您點擊Pin時顯示。