user2014551
user2014551

Reputation: 325

MKPinAnnotationView not working

I would like to have a MKMapView showing annotations with disclosure-buttons which lead to a view controller like the Golden Gate Bridge annotation in this Apple sample app.

I load the coordinates from a plist and the annotations appear correctly with title/subtitle but the method

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation

has no effect.

I guess that I somehow have to link the annotations with the pinannotations?

MapViewController.h:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "Annotation.h"

@interface MapViewController : UIViewController<CLLocationManagerDelegate, MKMapViewDelegate>

@property (strong, nonatomic) CLLocationManager *location;
@property (nonatomic, retain) NSArray *data;    
@end

MapViewController.m:

#import "MapViewController.h"

@interface MapViewController ()
@property (nonatomic, weak) IBOutlet MKMapView *mapView;
@end

@implementation MapViewController
@synthesize data;
@synthesize location, minLatitude, maxLatitude, minLongitude, maxLongitude;

- (void)viewDidLoad
{
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"City" ofType:@"plist"];
    self.data = [NSArray arrayWithContentsOfFile:dataPath];

    for (int i = 0; i < data.count; i++) {

        NSDictionary *dataItem = [data objectAtIndex:i];

        //Create Annotation
        Annotation *building = [[Annotation alloc] init];
        building.title = [dataItem objectForKey:@"Title"];
        building.subtitle = [dataItem objectForKey:@"Subtitle"];

        MKCoordinateRegion buildingcoordinates = { {0.0, 0.0}, {0.0, 0.0} };
        buildingcoordinates.center.latitude = [[dataItem objectForKey:@"Latitude"] floatValue];
        buildingcoordinates.center.longitude = [[dataItem objectForKey:@"Longitude"] floatValue];

        building.coordinate = buildingcoordinates.center;
        [self.mapView addAnnotation:building];

    }

    [super viewDidLoad];

}

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

        static NSString *pinIdentifier = @"pinIndentifier";

        MKPinAnnotationView *pinView = (MKPinAnnotationView *)
        [self.mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];
        if (pinView == nil)
        {
            // if an existing pin view was not available, create one
            MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc]
                                                  initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
            customPinView.pinColor = MKPinAnnotationColorPurple;
            customPinView.animatesDrop = YES;
            customPinView.canShowCallout = YES;

            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [rightButton addTarget:self
                            action:@selector(showDetails:)
                  forControlEvents:UIControlEventTouchUpInside];
            customPinView.rightCalloutAccessoryView = rightButton;

            return customPinView;
        }
        else
        {
            pinView.annotation = annotation;
        }
        return pinView;
}

Annotation.h:

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

@interface Annotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

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

@end

Annotation.m:

#import "Annotation.h"

@implementation Annotation

@synthesize coordinate, title, subtitle;

@end

Upvotes: 2

Views: 2881

Answers (1)

user467105
user467105

Reputation:

Most likely the map view's delegate is not set which means the viewForAnnotation delegate method will not get called.

Since you've declared mapView as an IBOutlet, in the xib, make sure that the map view's delegate is connected to File's Owner.

Alternatively, at the top of the viewDidLoad method in MapViewController, set it programmatically:

mapView.delegate = self;

Upvotes: 4

Related Questions