0

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

感谢您的时间。

视图控制器.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;
}

注释.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

注释.m

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

我试图添加第四行:

注释.h

@property (nonatomic, copy) NSString  *colortag;

注释.m

@synthesize coordinate, title, subtitle, colortag;

视图控制器.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

4

1 回答 1

0

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

于 2013-04-14T08:22:12.537 回答