我有 30 个注释,并且还在不断增加。正在寻找一种更简单的方法来编码吗?
我正在将多个注释编码到一个项目中。目前我有 30 个注释,并且还在不断增加。我想知道是否有一种更简单的方法必须为每个注释创建 annotation.h 和 annotation.m 类。
目前在我的地图视图控制器中,我创建注释对象并将它们放置在一个数组中,这对我来说效果很好,但正如您可以想象的那样,一旦您有大量注释,就需要管理大量代码,更不用说大量代码了类。
例如,其中一个注释类如下所示:
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
我正在考虑读取包含所有注释的 CSV 文件将是最好的方法,我选择的任何选项都会导致我重写了很多代码,这就是为什么我在做任何事情之前都会问这个问题。有人有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,MapCallouts 示例应用程序没有给出如何实现通用注释类的好示例。
实现 MKAnnotation 协议的类可以提供可设置的坐标属性或采用坐标的自定义 init 方法。
但是,由于您使用的是 iOS 4.0,更简单的选择是仅使用预定义的
MKPointAnnotation
类,提供您可以设置的属性。例如:注释数据当然可以来自任何地方,您可以循环数据以在地图上创建注释。
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 settablecoordinate
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:The annotation data can of course come from anywhere and you can loop through the data to create the annotations on the map.