【问题标题】:Conditional Custom Annotation View in iphone MapViewiphone MapView中的条件自定义注释视图
【发布时间】:2011-11-04 22:45:22
【问题描述】:

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

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

我使用此代码使图钉可点击:

pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

我需要在数据库中添加一个新列(可点击),并在“可点击”值为 ON 时使图钉可点击。

关于最佳创意的任何详细建议?

【问题讨论】:

  • 您使用什么类来创建 MKAnnotation 对象?自定义类或预定义的东西,如 MKPointAnnotation?
  • 我使用这个:MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:UserAnnotationId] autorelease]; customPinView.pinColor = MKPinAnnotationColorPurple; customPinView.animatesDrop = YES; customPinView.canShowCallout = YES; customPinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];返回 customPinView;

标签: iphone sqlite annotations mapkit


【解决方案1】:

根据我的经验,如果您不设置注释的任何属性(标题、子标题、图像、附件按钮)并点击图钉,则不会显示标注。 相反,如果你想在点击附件按钮时显示标注但不调用操作,你可以使用这样的东西:

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

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

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

当然我以字符串为例,你也可以使用NSNumbers 或BOOLs。

希望我理解你的问题。

【讨论】:

  • 我想隐藏 UIButtonTypeDetailDisclosure,所以我想我需要先使用它,对吧?
  • 当尝试读取这行代码时程序崩溃: NSString *clickable=[[yourArray objectAtIndex:yourIndex] objectForKey:@"clickable"];知道我正确放置了数组和索引。
  • 这只是你可以做的一个例子,很明显会崩溃。如果要隐藏按钮,请不要设置。
【解决方案2】:

要根据表格中的“可点击”标志来控制标注是否有附属按钮,我建议你在你的注解类中添加一个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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多