iphone MapView中的条件自定义注释视图

时间:2011-11-04 22:45:22

标签: iphone sqlite annotations mapkit

在我的MapView中,我从SQLite读取数据并在其上显示引脚(最多5000条记录)。

数据库具有ID |的结构经度|纬度|标题|字幕

我使用此代码使引脚可点击:

pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

我需要在数据库中添加一个新列(Clickable),并在“Clickable”值为ON时使该引脚可单击。

关于做到这一点的最佳想法的任何详细建议?

2 个答案:

答案 0 :(得分:2)

根据我的经验,如果您未设置注释的任何属性(标题,副标题,图像,附件按钮)并点按图钉,则不会显示标注。 相反,如果您想要显示标注,但在点击附件按钮时不调用动作,您可以使用以下内容:

(从数据库下载数据后,您可以将每个项目存储为NSDictionary,然后将所有项目存储在NSArray

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{

    NSString *clickable=[[yourArray objectAtIndex:yourIndex] objectForKey:@"clickable"];
    if(![clickable isEqualToString:@"YES"]){
        return;
    }
}

当然我以字符串为例,您也可以使用NSNumberBOOL s。

希望我理解你的问题。

答案 1 :(得分:0)

为了控制标注是否具有基于表中“可点击”标志的附件按钮,我建议您在注释类中添加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);
}