0

I have made these pieces of code for my custom annotation:

    - (void)viewDidLoad

{

    [super viewDidLoad];
mapView.delegate=self;

    [self AddLocations];

    [self initiateMap];

......

this is my addLocation function:

-(id<MKAnnotation>)addAnnotationWithTitle: (NSString*) title coordinate:(CLLocationCoordinate2D)

Location imageName:(NSString*) imageName{

    CustomAnnotation *annotation = [[CustomAnnotation alloc]init];

    annotation.title = title;

    annotation.subtitle = @"This is a subtitle";

    [annotation setCoordinate:Location];

    annotation.imageName = imageName;



    return annotation;

}

and my CustomAnnotation Class:

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

    NSLog(@"Test");
    if([annotation isKindOfClass:[MKUserLocation class]]){
        return nil;
    }

    if([annotation isKindOfClass:[CustomAnnotation class]]){
        CustomAnnotation *customAnnotation = annotation;
        static NSString* annotationid = @"Annotation";
        MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationid];
        if(!annotationView){
            annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationid];
            annotationView.canShowCallout = YES;
            annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }else{
            annotationView.annotation = annotation;
        }
        annotationView.image = [UIImage imageNamed:customAnnotation.imageName];
        return annotationView;

    }
    return nil;
}

For some reason the viewForAnnotation function is never used. Can anyone help me out here? Thanks

Black Magic
  • 2,706
  • 5
  • 35
  • 58
  • Refer to this link : http://stackoverflow.com/questions/5163493/mkannotationview-mapview-not-getting-called – Divyu May 13 '13 at 11:21
  • 1
    where viewForAnnotation method located? Is it in customAnnotation class? – wesley May 13 '13 at 11:31
  • 2
    you have to add this viewForAnnotation delegate method where you set mapView delegate as self.I mean your viewController.m where you added the webView. In your customAnnotation class should do only get and set the coordinates,title and subTitle. – wesley May 13 '13 at 12:11
  • I don't see where are you add annotation to map? Something like [self.mapView addAnnotation:annotation]; – nerowolfe May 13 '13 at 11:19
  • Well, I put them in an array and then I add all the annotations ;) – Black Magic May 13 '13 at 11:24
  • self.mapView.delegate = self; is there? There, I see – nerowolfe May 13 '13 at 11:50
  • Yet, it sadly still doesn't work :s – Black Magic May 13 '13 at 13:16
  • Is your `IBOutlet` hooked up? Also, you're setting `mapView.delegate = self` (meaning you're referencing an instance variable, not a property accessor method). Are you sure `mapView` is non-`nil` (either because of `IBOutlet` not hooked up or following issue)? In your header, did you define both `@property` and ivar? Or just the `@property`? (It's no longer recommended to define instance variables for your properties, but rather let the compiler synthesize those automatically for you, to eliminate the possibility of misalignment between properties and backing instance variables.) – Rob May 13 '13 at 13:52

0 Answers0