【问题标题】:Add type to annotation so I can filter Annotation array in the 3 pin colors将类型添加到注释,以便我可以过滤 3 种引脚颜色的注释数组
【发布时间】:2013-04-12 16:53:01
【问题描述】:

我是 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

【问题讨论】:

    标签: annotations mkmapview mapkit mkannotation xcode4.6


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多