我有 30 个注释,并且还在不断增加。正在寻找一种更简单的方法来编码吗?

发布于 2024-11-07 17:35:11 字数 945 浏览 3 评论 0原文

我正在将多个注释编码到一个项目中。目前我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的背包 2024-11-14 17:35:11

不幸的是,MapCallouts 示例应用程序没有给出如何实现通用注释类的好示例。

实现 MKAnnotation 协议的类可以提供可设置的坐标属性或采用坐标的自定义 init 方法。

但是,由于您使用的是 iOS 4.0,更简单的选择是仅使用预定义的 MKPointAnnotation 类,提供您可以设置的属性。例如:

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

注释数据当然可以来自任何地方,您可以循环数据以在地图上创建注释。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文