我在下面的代码中完美地使用了一个独特的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];
如果你想要多个pin和annotaion复制下面的CLLocation实例并更改以下属性
CLLocationCoordinate2D secondLocation = CLLocationCoordinate2DMake(another latitude, another longitude);
secondAnnotation = [[MyAnnotationPins alloc]
initWithCoordinate:secondLocation
title:@"Second Title"
subtitle:@"Second subtitle"];
[mapView addAnnotation:secondAnnotation]; <code>
依此类推第三,第五,等等。不要忘记在你的视图控制器上创建secondLocation proerty,就像在SecondViewController.h中的第一个一样,也是 @synthesize SecondViewController.m文件中的secondAnnotation属性
@property (strong, nonatomic) MyAnnotationPins* secondAnnotation;
答案 0 :(得分: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];
}
当您浏览列表时,您可以获得每个列表的正确坐标和标题。这样,列表可以增长或缩小,您不需要添加第三,第四或第五个注释属性。