0

I´m creating a view with a Map and an annotation using the following class:

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

@interface ContactDetailAnnotation : NSObject <MKAnnotation>

- (id) initWithLatitude:(CLLocationDegrees) lat longitude:(CLLocationDegrees) lng;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coordinateArg;

@property (unsafe_unretained) CLLocationDegrees latitude;
@property (unsafe_unretained) CLLocationDegrees longitude;
@property (unsafe_unretained, nonatomic) CLLocationCoordinate2D coordinate;

@end

The problem is that the method:

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

used to draw my Point isn´t being trigered. If i use

self.myMap.showsUserLocation = TRUE;    

The method is trigered, so the problem must be in the class i created.

My .m

#import "ContactDetailAnnotation.h"

@implementation ContactDetailAnnotation

@synthesize latitude = _latitude, longitude = _longitude, coordinate = _coordinate;

- (id) initWithCoordinate:(CLLocationCoordinate2D) coordinateArg
{
    self.coordinate = coordinateArg;

    return self;
}

- (id) initWithLatitude:(CLLocationDegrees)lat longitude:(CLLocationDegrees)lng 
{
    self.latitude = lat;
    self.longitude = lng;

    return self;
}

- (CLLocationCoordinate2D) coordinate 
{
CLLocationCoordinate2D coord = {self.latitude, 
                                self.longitude};
return coord;
}

@end
bruno
  • 2,154
  • 5
  • 39
  • 64

1 Answers1

0

Did you add your annotation to the mapview's annotations array?

Edit

Here's a bare-bones implementation that is working for me:

MyAnnotation.h:

#import <Foundation/Foundation.h>
#import <Mapkit/Mapkit.h>

@interface MyAnnotation : NSObject <MKAnnotation>
{
    CLLocationCoordinate2D coordinate;
}

@property (nonatomic) CLLocationCoordinate2D coordinate;

- (id) initWithCoordinate:(CLLocationCoordinate2D)aCoordinate;

@end

MyAnnotation.m

#import "MyAnnotation.h"

@implementation MyAnnotation

@synthesize coordinate;

- (id) initWithCoordinate:(CLLocationCoordinate2D)aCoordinate
{
    coordinate = aCoordinate;
    return self;
}

@end

My View Controller:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CLLocationCoordinate2D coordinate = {42.149,-74.9384};
    MyAnnotation *myAnnotation = [[MyAnnotation alloc] initWithCoordinate:coordinate];
    [self.mapView addAnnotation:myAnnotation];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
{
    MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"Identifier"];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Identifier"];
    }
    return annotationView;
}
nevan king
  • 112,709
  • 45
  • 203
  • 241
  • Yes [self.myMap addAnnotation:self.ContactDetailAnnotation]; – bruno Apr 16 '12 at 21:10
  • Then try taking out all those init methods and instance variables and just set the coordinate using `setCoordinate:`. Also, log the annotation in the `annotations` array to see what it contains. – nevan king Apr 16 '12 at 21:18
  • Oh, and using that `initWithLatitude:longitude:` won't actually set the important part of the annotation, it's `coordinate` property. And I think that you'd have to call `[super init]` at some stage. – nevan king Apr 16 '12 at 21:19
  • If i set the coordinate like this: [someAnnotation setCoordinate:location]; i will give the following error -> [ContactDetailAnnotation setCoordinate:]: unrecognized selector sent to instance 0x648b400 – bruno Apr 16 '12 at 21:47