1

我下面的这段代码完美地适用于一个独特的 Pin 和一个注释。我想在不进行太多更改的情况下对其进行调整,以显示更多 Pin 图、位置和注释。

有一个MyAnnotationPins用以下几行调用的类:

MyAnnotationPins.h

@interface MyAnnotationPins : NSObject < MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D)annotCoordinate title:(NSString*)annotTitle subtitle:(NSString*)annotSubtitle;

MyAnnotationPins.m

@synthesize coordinate;
@synthesize subtitle;
@synthesize title;
-(id)initWithCoordinate:(CLLocationCoordinate2D)annotCoordinate title:(NSString*)annotTitle subtitle:(NSString*)annotSubtitle
{
    self = [super init];
    if (self)
    {
        coordinate = annotCoordinate;
        subtitle = [[NSString alloc] initWithString:annotSubtitle];
        title = [[NSString alloc] initWithString:annotTitle];
    }

    return self;
}

在视图控制器中,以下代码:

SecondViewController.h

import "MyAnnotationPins.h"


@interface SecondViewController : UIViewController < MKMapViewDelegate > 

@property (weak, nonatomic) IBOutlet MKMapView *mapView;    
@property (strong, nonatomic) MyAnnotationPins* biblioAnnotation;

在 SecondViewController.m 中的实现:

- (void)viewDidLoad
{

    [super viewDidLoad];

    mapView.delegate = self;

    MKCoordinateRegion mapRegion;
    mapRegion.center.latitude=-18.924129;
    mapRegion.center.longitude=-48.283963;
    mapRegion.span.latitudeDelta=0.2;
    mapRegion.span.longitudeDelta=0.2;

    [mapView setRegion:mapRegion animated:YES];

    ///// This is just for One annotaion/Pin on Map /////
    CLLocationCoordinate2D parliamentLocation = CLLocationCoordinate2DMake(-18.924129, -48.283963);
    biblioAnnotation = [[MyAnnotationPins alloc]
                            initWithCoordinate:parliamentLocation
                            title:@"Ponto Biblioteca"
                            subtitle:@"Taxi proximo"];
    [mapView addAnnotation:biblioAnnotation];

如果您想要多个引脚和注释,请复制下面的 CLLocation 实例并更改以下属性

CLLocationCoordinate2D secondLocation = CLLocationCoordinate2DMake(another latitude, another longitude);
secondAnnotation = [[MyAnnotationPins alloc]
                            initWithCoordinate:secondLocation
                            title:@"Second Title"
                            subtitle:@"Second subtitle"];
[mapView addAnnotation:secondAnnotation];  <code>  

依此类推,第三个,第四个第五个等等。不要忘记在您的视图控制器上创建 secondLocation 属性,就像 SecondViewController.h 中的第一个属性以及 SecondViewController.m 文件中的 @synthesize secondAnnotation 属性

@property (strong, nonatomic) MyAnnotationPins* secondAnnotation;
4

1 回答 1

0

像这样将它们添加到循环中

for(X in Y)
{
 CLLocationCoordinate2D parliamentLocation = CLLocationCoordinate2DMake(-18.924129, -48.283963);
MyAnnotationPins* biblioAnnotation = [[MyAnnotationPins alloc]
                        initWithCoordinate:parliamentLocation
                        title:@"Ponto Biblioteca"
                        subtitle:@"Taxi proximo"];
[mapView addAnnotation:biblioAnnotation];
}

当您遍历列表时,您可以获得每个列表的正确坐标和标题。这样您的列表可以增长或缩小,您不需要添加第三、第四或第五个注释属性。

于 2012-09-15T03:46:13.570 回答