我有30个注释,并且在不断增长。寻找一种更简单的代码编码方式? [英] I have 30 annotations, and growing. Looking for a simpler way to code this?
问题描述
我正在将多个注释编码到项目中。目前我有30个注释,并且还在增长。我想知道是否有一种简单的方法来为每个单独的注释创建一个annotation.h和annotation.m类。
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:
//Annotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Annotation : NSObject {
}
@end
Annotation.m:
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
我正在考虑阅读在包含所有注释的CSV文件中,这是最好的方法,我选择的任何选项都会导致我重写大量代码,这就是为什么我在做任何事之前都会问这个问题。有没有人有任何建议?
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?
推荐答案
遗憾的是,MapCallouts示例应用程序并未提供如何实现通用注释的良好示例类。
The MapCallouts sample app unfortunately doesn't give a good example of how to implement a generic annotation class.
实现 MKAnnotation
协议的类可以提供可设置的坐标
属性或采用坐标的自定义init方法。
Your class that implements the MKAnnotation
protocol can provide a settable coordinate
property or a custom init method that takes the coordinates.
但是,由于您使用的是iOS 4.0,因此更简单的选择就是使用预定义的 MKPointAnnotation
类,提供您可以设置的属性。例如:
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.
这篇关于我有30个注释,并且在不断增长。寻找一种更简单的代码编码方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!