Edward Hasted
Edward Hasted

Reputation: 3433

MapKit pin title not showing

The location is working but the title isn't appearing, most strange.

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

and 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 release] is remmed out to keep the ARC happy :-) Any ideas?

Upvotes: 1

Views: 1202

Answers (3)

Edward Hasted
Edward Hasted

Reputation: 3433

This did the trick:

[mapView selectAnnotation:newAnnotation animated:YES];

previously the title would only show if you clicked on the Pin.

Upvotes: 1

user467105
user467105

Reputation:

That code looks fine (except for the non-standard implementation of the init method).

The most likely reason the title (callout) isn't appearing is that in your viewForAnnotation delegate method, you are not setting canShowCallout to YES (it defaults to NO).

In the viewForAnnotation delegate method, after you create the MKAnnotationView, set canShowCallout to YES.


Unrelated to the title/callout not showing, the init method in your MapViewAnnotation class should be implemented like this:

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

Upvotes: 0

NANNAV
NANNAV

Reputation: 4901

you call annotation delegates refer this link, mapkit-example-in-iphone

Upvotes: 0

Related Questions