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?
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.
Perhaps create an array of dictionary items of annotations (lat,lon,title,subtitle) and store in a plist?
Or maybe use the core data framework and store the items in a sqlite db?
Maybe this link will also help? Display a limited number of sorted annotations in Mapview
来源:https://stackoverflow.com/questions/6011931/i-have-30-annotations-and-growing-looking-for-a-simpler-way-to-code-this