添加类型到注释,以便我可以过滤3针颜色的注释数组

时间:2013-04-12 16:53:01

标签: annotations mkmapview mapkit mkannotation xcode4.6

我是xcode的新手 - 我制作了一张地图并使用MutableArray添加了一些注释。我想将它们分成3组,并使用3种不同的引脚颜色在地图上显示它们。 希望你可以帮助或给我一个可以帮助我的指南的网址。我尝试制作'if语句'和'annotationType',但可以让它正常工作。希望你能帮忙或给我一个暗示。

感谢您的时间。

ViewController.m

{

[super viewDidLoad];

[self.myMapView setDelegate:self];


MKCoordinateRegion myRegion;

CLLocationCoordinate2D center;
center.latitude = AMAR_LATITUDE;
center.longitude = AMAR_LONGITUDE;

MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;

myRegion.center = center;
myRegion.span = span;

[myMapView setRegion: myRegion animated: YES];

NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Annotation *myAnn;
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Annotation *myAnn;

myAnn = [[Annotation alloc] init];
location.latitude = TATU_LATITUDE;
location.longitude = TATU_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"Tante Tuli";
myAnn.subtitle =@"Amagerbrogade 161";
[locations addObject:myAnn];
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

MKPinAnnotationView *view =[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
view.pinColor = MKPinAnnotationColorRed;
view.enabled = YES;
view.animatesDrop = YES;
view.canShowCallout = YES;
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return view;
}

Annotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Annotation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
- initWithPosition:(CLLocationCoordinate2D)coords;
@end

Annotation.m

#import "Annotation.h"
@implementation Annotation;
@synthesize coordinate, title, subtitle;
- initWithPosition:(CLLocationCoordinate2D)coords; {
if (self = [super init]) {
    self.coordinate = coords;
}
return self;
}
@end

我试图添加第四行:

Annotation.h

@property (nonatomic, copy) NSString  *colortag;

Annotation.m

@synthesize coordinate, title, subtitle, colortag;

ViewController.m

myAnn = [[Annotation alloc] init];
location.latitude = TATU.LATITUDE;
location.longitude = TATU.LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"name";
myAnn.subtitle =@"adr";
myAnn.colortag =@"purple";
[locations addObject:myAnn];

if ([[annotation colortag] isEqualToString:@"purple"])
view.pinColor = MKPinAnnotationColorPurple;

我得到错误:没有已知的选择器'colertag'的实例方法:S

1 个答案:

答案 0 :(得分:0)

您的Annotation班级有标题,副标题和坐标属性。只需添加第四个并在设置其他时设置它,然后读取它并在viewForAnnotation中设置颜色。在阅读新属性之前,您需要将annotation参数强制转换到您的类中(在检查它是您的类的实例而不是用户的位置注释之后)。