3

I am coding multiple annotations into a project. Currently I have 30 annotations, and growing. I'm wondering if there is a simplier way of having to create a annotation.h and annotation.m classes for each single annotation.

Currently in my map view controller, I create the annotation objects and place them in an array, which has been working well for me but as you could imagine, its a lot of code to manage once you have tons of annotations, not to mention tons of classes.

So for example, one of the annotation classes looks like this:

Annotation.h:

//Annotation.h

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

@interface Annotation : NSObject {

}

@end

Annotation.m:

//Annotation.m

#import "Annotation.h"

@implementation Annotation

-(CLLocationCoordinate2D)coordinate;
{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = -45.866416;
    theCoordinate.longitude = 170.519931;
    return theCoordinate; 
}

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

-(NSString *)subtitle
{
    return @"Subtitle";
}

-(void)dealloc
{
    [super dealloc];
}

@end

I'm thinking of reading in a CSV file with all the annotations would be the best way to go, any option I choose will result in me rewriting a lot of code, which is why I'm asking this question before I do anything. Does anyone have any suggestions?

Anton Smith
  • 1,368
  • 2
  • 10
  • 15
  • Can you give an example of two annotation .h files you created? What's different about each class? Why do you think you need to create a separate class for each annotation? –  May 15 '11 at 23:36
  • sure, just did. Each annotation class has unique cllocationcoordinates, title and subtitles. I'm following the MapCallouts project provided by Apple, so this is why I've created separate classes for annotations. – Anton Smith May 15 '11 at 23:46
  • Why don't you just create a class which allows you to specify the coordinates, title, and subtitle, and just create 30 instances? – ughoavgfhw May 16 '11 at 00:10

1 Answers1

3

The MapCallouts sample app unfortunately doesn't give a good example of how to implement a generic annotation class.

Your class that implements the MKAnnotation protocol can provide a settable coordinate property or a custom init method that takes the coordinates.

However, since you're using iOS 4.0, an easier option is to just use the pre-defined MKPointAnnotation class that provides properties that you can set. For example:

MKPointAnnotation *annot = [[MKPointAnnotation alloc] init];
annot.title = @"Title";
annot.subtitle = @"Subtitle";
annot.coordinate = CLLocationCoordinate2DMake(-45.866416, 170.519931);
[mapView addAnnotation:annot];
[annot release];

The annotation data can of course come from anywhere and you can loop through the data to create the annotations on the map.

  • Thank you, this is exactly what I am looking for. I have decided to place the data in a SQlite database, and your code in a for loop would be perfect. Cheers – Anton Smith May 16 '11 at 00:49
  • This code does not allow me to create unique custom pins for each annotation. How would I accomplish this in the viewForAnnotation method without hard coding if statements in the MapCallouts example? – Anton Smith May 29 '11 at 12:35
  • I'd suggest creating a custom class that implements MKAnnotation (or subclass MKPointAnnotation) and add custom properties to it. You can then access those custom properties in viewForAnnotation and respond accordingly. These other answers might help: http://stackoverflow.com/questions/6146984/adding-different-images-to-different-annotation-views-in-xcode/6147294#6147294, http://stackoverflow.com/questions/5939223/store-data-in-mkannotation/5940563#5940563, and http://stackoverflow.com/questions/4841753/custom-mkannotation-class-for-changing-leftcalloutannotationview/4843134#4843134. –  May 29 '11 at 14:59
  • By the way, that custom class would not have the coordinates hard-coded in it (like the MapCallouts example). It might add a custom property like "image" which you set before doing addAnnotation. –  May 29 '11 at 15:01
  • Alternatively, please give more details on what is meant by "unique custom pin" (what will be different about each pin) and I can give a better answer. –  May 29 '11 at 15:08