0

Should we do so in - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

Is there some sample code on proper use of

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
Anonymous White
  • 2,149
  • 3
  • 20
  • 27

1 Answers1

1

Yes you can do it in viewForAnnotation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{        
    MKAnnotationView *annotationView = (MKAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:@"myIdentifier"];
    if (annotationView == nil) 
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } 
    else 
    {
        annotationView.annotation = annotation;
    }
    annotationView.image = [UIImage imageNamed:@"myPinImage.png"];
    // set your offset here
    annotationView.centerOffset = CGPointMake(offsetX, offsetY);
    return annotationView;
} 
Ken Toh
  • 3,721
  • 1
  • 24
  • 30
  • I think you should do annotationView.annotation=annotation too even if annotationView return nil and you recreate them. Or am i correct here? – Anonymous White Oct 31 '12 at 03:16
  • Actually I was wrong. You already use initWithAnnotation. So the annotationView already know where the stuff are. – Anonymous White Oct 31 '12 at 05:28
  • oh I misread your previous comment. Yea you don't have to set the annotation property again. Anyway I moved the setting of the image out of the "if" body. That was my earlier mistake that I thought you were pointing out in your previous comment. – Ken Toh Oct 31 '12 at 05:33