Azhar
Azhar

Reputation: 20680

ios: mapKit viewForAnnotation disappears popup Title from PIN?

I am trying to create draggable custom pin with Title popup with MapKit. but when I implement viewForAnnotation to make PIN appear as custom image, its disappears pop title as if I youch Pin on map it does not show titla popup,

while if I comments viewForAnnotation it works fine, but I want both things custom image and popup title when tap pin.

below is code

- (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

Upvotes: 1

Views: 960

Answers (1)

Azhar
Azhar

Reputation: 20680

Just put annotationView.canShowCallout= YES; and it will work

updated code

-(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;
}

Upvotes: 3

Related Questions