要控制标注是否具有基于表中“可点击”标志的附件按钮,我建议您将clickable
属性添加到您的标注类并在添加标注时进行设置。然后,您可以在viewForAnnotation
方法中创建注释视图时检查此属性。
要将注释添加到地图视图(即当你调用addAnnotation
),如果你正在使用预先定义的类一样MKPointAnnotation
,你需要,而不是定义自己的自定义类,它实现了MKAnnotation
协议。如果您已有自定义班级,请为其添加clickable
属性。
这里有一个自定义的注释类的例子:
//CustomAnnotation.h...
@interface CustomAnnotation : NSObject<MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, assign) int annotationId;
@property (nonatomic, assign) BOOL clickable;
@end
//CustomAnnotation.m...
@implementation CustomAnnotation
@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
@synthesize annotationId;
@synthesize clickable;
- (void)dealloc {
[title release];
[subtitle release];
[super dealloc];
}
@end
这里是你将如何创建并添加注释的例子(注记的属性的实际值将来自您的SQL行):
CustomAnnotation *ca1 = [[CustomAnnotation alloc] init];
ca1.annotationId = 1;
ca1.coordinate = CLLocationCoordinate2DMake(lat1,long1);
ca1.title = @"clickable";
ca1.subtitle = @"clickable subtitle";
ca1.clickable = YES;
[myMapView addAnnotation:ca1];
[ca1 release];
CustomAnnotation *ca2 = [[CustomAnnotation alloc] init];
ca2.annotationId = 2;
ca2.coordinate = CLLocationCoordinate2DMake(lat2,long2);
ca2.title = @"not clickable";
ca2.subtitle = @"not clickable subtitle";
ca2.clickable = NO;
[myMapView addAnnotation:ca2];
[ca2 release];
然后viewForAnnotation
看起来就像这样:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *reuseId = @"CustomAnnotation";
MKPinAnnotationView* customPinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (customPinView == nil) {
customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
}
else {
customPinView.annotation = annotation;
}
CustomAnnotation *ca = (CustomAnnotation *)annotation;
if (ca.clickable)
customPinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
else
customPinView.rightCalloutAccessoryView = nil;
return customPinView;
}
最后,你可以处理在calloutAccessoryControlTapped
委托方法按下按钮,并访问您的自定义注解的属性:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
CustomAnnotation *ca = (CustomAnnotation *)view.annotation;
NSLog(@"annotation tapped, id=%d, title=%@", ca.annotationId, ca.title);
}
是您使用的类来创建MKAnnotation对象?一个自定义类或类似MKPointAnnotation的预定义? – Anna
我使用此: MKPinAnnotationView * customPinView = [[[MKPinAnnotationView的alloc] \t \t \t \t \t \t \t \t \t \t \t \t initWithAnnotation:注释reuseIdentifier:UserAnnotationId]自动释放]; customPinView.pinColor = MKPinAnnotationColorPurple; customPinView.animatesDrop = YES; customPinView.canShowCallout = YES; customPinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; return customPinView; – asnanak