0

I'm working on an app that makes use of the MapKit Classes Mkannotation and MKAnnotationView (Both subclassed, of course).

My question is, how can I put multiple FGAnnotationViews on my map (about 5) that all have different images?

I know that I could create 5 different new Classes and init the one matching, but I thought, that maybe there was a method to put an if statement inside the

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

function, like

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView *annView = nil;

if (annotation == myRestaurantAnnotation) {

    FGAnnotationView *fgAnnView = (FGAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"Location"];
    fgAnnView = [[FGAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Location"];
}
return annView;
}

Anyone?

Finn Gaida
  • 4,000
  • 4
  • 20
  • 30

1 Answers1

2

You're almost there. Try this

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    MKAnnotationView *annView = nil;

    FGAnnotationView *fgAnnView = (FGAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"Location"];

    if (annotation == myRestaurantAnnotation) {
        fgAnnView.image = //MAGIC GOES HERE//;
    } else if (annotation == myBankAnnotation) {
        fgAnnView.image = //MAGIC GOES HERE//;
    }
    return fgAnnView;
}

If you are letting the map draw the user's location you should check the class of the annotation and make sure it isn't MKUserLocation. If it is then you return nil. If you have multiple instances of each annotation class you can use the class to determine the image to set rather than matching the object itself, like this:

if ([annotation isKindOfClass:[MKUserLocation class]]) {
    return nil;
} else if ([annotation isKindOfClass:[FGRestaurantAnnotation class]]) {
    fgAnnView.image = //YOUR IMAGE CODE//
}
Craig
  • 8,093
  • 8
  • 42
  • 74
  • Well that sounds logical to me, but I'm still getting an error 'use of undeclared identifier -- myAnnotation --' or 'Reciever FGRestaturantAnnotation for class message is a forward declaration' for the second method... – Finn Gaida Nov 27 '12 at 05:57
  • Do you have a variable called "myAnnotation" if not then you can't use it in a == comparison. And if your FGRestaturantAnnotation class is not included/imported at the top of your viewcontroller then you can refer to it in the code. – Craig Nov 27 '12 at 07:44