2011-11-04 115 views
0

在我的MapView中,我从SQLite读取数据并在其上显示引脚(最多5000条记录)。iphone中的条件自定义注释视图MapView

该数据库具有ID |经度|纬度|标题|字幕

我用这个代码,以使该引脚可点击:

pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

我需要在数据库中添加一个新的列(点击),使引脚可点击只是如果“可点击”值是ON 。

关于最好的想法做任何详细的建议吗?

+0

是您使用的类来创建MKAnnotation对象?一个自定义类或类似MKPointAnnotation的预定义? – Anna

+0

我使用此: 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

回答

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; 
    } 
} 

当然,我用一个字符串作为例子,你也可以使用NSNumber S或BOOL秒。

希望我明白你的问题。

+0

我想隐藏UIButtonTypeDetailDisclosure,所以我想我需要使用它之前,对吧? – asnanak

+0

和程序crach当试图读取这行代码: NSString * clickable = [[yourArray objectAtIndex:yourIndex] objectForKey:@“clickable”];知道我正确地把我的数组和索引, 。 – asnanak

+0

这只是你可以做的一个例子,很明显,它会崩溃。如果你想隐藏按钮,不要设置它。 – Mat

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); 
}