ios:mapKit viewForAnnotation从PIN中弹出标题?

时间:2014-02-13 13:20:19

标签: ios iphone objective-c ipad mapkit

我正在尝试使用带有 MapKit 的标题弹出窗口创建可拖动自定义图钉。但是当我实现 viewForAnnotation 以使PIN显示为自定义图像时,它会消失流行标题,就好像我在地图上它没有显示titla popup,

如果我评论 viewForAnnotation ,它可以正常工作,但我想在点按pin时自定义图像和弹出标题。

下面是代码

- (void)viewDidLoad
{
     MKCoordinateRegion newRegion = MKCoordinateRegionMakeWithDistance(LocCoordinate, 20000, 20000); 
     DDAnnotation *point = [[DDAnnotation alloc] init];
     [point setCoordinate:mapCenter];
     [point setTitle:@"Where am I"];
     [point setSubtitle:@"I am here"];

     [self.mvTripMap addAnnotation:point];        
     [self.mvTripMap setRegion:newRegion animated:YES];

     [super viewDidLoad];
}

-(MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation{
    static NSString *cabAnnotationIdentifier=@"cabAnnotationIdentifier";
    MKAnnotationView *annotationView=[MapView dequeueReusableAnnotationViewWithIdentifier:cabAnnotationIdentifier];
    if(!annotationView){            
        annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:cabAnnotationIdentifier];
        annotationView.image=[UIImage imageNamed:@"start.png"];             
        annotationView.draggable = YES; 
    }
    return annotationView;
}

DDAnnotation.h

#import <MapKit/MapKit.h>

@interface DDAnnotation : MKPlacemark {
    CLLocationCoordinate2D coordinate_;
    NSString *title_;
    NSString *subtitle_;
}

// Re-declare MKAnnotation's readonly property 'coordinate' to readwrite.
@property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;

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

@end

DDAnnotation.m

#import "DDAnnotation.h"

@implementation DDAnnotation

@synthesize coordinate = coordinate_;
@synthesize title = title_;
@synthesize subtitle = subtitle_;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary {

    if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary])) {
        self.coordinate = coordinate;
    }
    return self;
}

@end

1 个答案:

答案 0 :(得分:3)

只需添加annotationView.canShowCallout= YES;即可使用

更新代码

-(MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation{
    static NSString *cabAnnotationIdentifier=@"cabAnnotationIdentifier";
    MKAnnotationView *annotationView=[MapView dequeueReusableAnnotationViewWithIdentifier:cabAnnotationIdentifier];
    if(!annotationView){            
        annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:cabAnnotationIdentifier];
        annotationView.image=[UIImage imageNamed:@"start.png"];             
        annotationView.draggable = YES; 
        annotationView.canShowCallout= YES;
    }
    return annotationView;
}