2

I am able to load a big list of location onto my MapKit and display them all with a custom Pin image and annotation.

The issue i'm having is that I currently have all annotations displaying the same Title, Subtitle, and pinImage.

How do I make it so that I can set each annotation with its own Title and different Pin image? I'm having a hard time trying to identify what annotation is being setup via mapView:viewForAnnotation.

- (NSString *)subtitle{

    return @"This is the annotation subtitle.";
}

- (NSString *)title{
    return @"Annotations Title";
}

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if (annotation == mapView.userLocation) {
        return nil;
    }

    MKAnnotationView *annView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    UIImage *pinImage = [UIImage imageNamed:@"mapPin.png"];
    annView.image = pinImage;
    return annView;
}
MikesTooLz
  • 53
  • 2
  • 5
  • similar question with code http://stackoverflow.com/questions/3054374/how-to-get-the-title-and-subtitle-for-a-pin-when-we-are-implementing-the-mkannota – Aaron Saunders Sep 27 '10 at 03:56
  • actually you can return a different annotation by replacing the line `return nil;` – Raptor Jan 05 '12 at 11:53

1 Answers1

2

You'll need to cast the annotation object to your custom type and access it's properties to appropriately change the annotationView that you return.

Apple's WeatherMap sample project is a good example of how to do this. https://developer.apple.com/library/ios/#samplecode/WeatherMap/Introduction/Intro.html

The MapCallouts sample project is the same idea, but a simpler implementation. https://developer.apple.com/library/ios/#samplecode/MapCallouts/Introduction/Intro.html

Kris Markel
  • 12,142
  • 3
  • 43
  • 40
  • Hey, thanks a ton. I got my app working with a combination of the two and I got it doing more then I originally planned. Thanks. – MikesTooLz Sep 28 '10 at 00:25