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

发布于 2024-12-13 16:11:40 字数 303 浏览 2 评论 0原文

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

数据库的结构为 ID|经度|纬度 |标题 | 我使用此代码

使图钉可点击:

pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

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

关于这样做的最佳想法有什么详细建议吗?

In my MapView, i read data from SQLite and display pins on it (up to 5000 record).

the database has the structure of ID| Longitude| Latitude | Title | subtitle

i used this code to make the pin clickable:

pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

i need to add a new column (Clickable) in the database, and make the pin clickable just if the "Clickable" value is ON.

any detailed suggestion about the best idea to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻许诺言 2024-12-20 16:11:40

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

从数据库下载数据后,您可以将每个项目存储为 NSDictionary code> 然后是 NSArray 中的所有项目)

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

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

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

希望我理解你的问题。

From my experience if you don't set any property of the annotation (title,subTitle,image,accessory-button) and tap on the pin, the callout is not displayed.
Instead, if you want show the callout but not call an action when the accessory button is tapped, you could use a thing like this:

(After downloading the data from the db, you could store each item as a NSDictionary and then all items in a NSArray)

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

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

Of course I used a string as example, you may also use NSNumbers or BOOLs.

Hope i understood your question.

倥絔 2024-12-20 16:11:40

要根据表中的“clickable”标志控制标注是否具有附件按钮,我建议您向注释类添加一个 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);
}

To control whether the callout has the accessory button based on the "clickable" flag in the table, I suggest you add a clickable property to your annotation class and set it when adding the annotation. Then you can check this property when creating the annotation view in the viewForAnnotation method.

To add the annotations to the map view (ie. when you call addAnnotation), if you are currently using a pre-defined class like MKPointAnnotation, you'll need to instead define your own custom class that implements the MKAnnotation protocol. If you already have a custom class, add a clickable property to it.

Here's an example of a custom annotation class:

//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

Here's an example of how you would create and add the annotations (the actual values for the annotation properties will come from your sql rows):

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

Then the viewForAnnotation will look like this:

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

Finally, you can handle the button press in the calloutAccessoryControlTapped delegate method and access your custom annotation's properties:

-(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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文