【问题标题】:How to create Custom MKAnnotationView and custom annotation title and subtitle如何创建自定义 MKAnnotationView 和自定义注释标题和副标题
【发布时间】:2013-04-21 14:11:44
【问题描述】:

我需要在 MKMapView 上创建上面的 Annotation 视图。我能够创建自定义注释视图,但是在点击注释时,视图需要打开带有大文本的图像,我无法创建那个。请提供一些链接或完成此任务的方法。

【问题讨论】:

    标签: ios map mkannotation iphone-5 mkannotationview


    【解决方案1】:

    要创建自定义注释视图(替换标准引脚),您只需在viewForAnnotation 方法中设置MKAnnotationViewimage 属性:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        if ([annotation isKindOfClass:[MKUserLocation class]])
        {
            return nil;
        }
        else if ([annotation isKindOfClass:[YourAnnotationClassHere class]]) // use whatever annotation class you used when creating the annotation
        {
            static NSString * const identifier = @"MyCustomAnnotation";
    
            MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    
            if (annotationView)
            {
                annotationView.annotation = annotation;
            }
            else
            {
                annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                              reuseIdentifier:identifier];
            }
    
            annotationView.canShowCallout = NO; // set to YES if using customized rendition of standard callout; set to NO if creating your own callout from scratch
            annotationView.image = [UIImage imageNamed:@"your-image-here.png"];
    
            return annotationView;
        }
        return nil;
    }
    

    您可能还想调整centerOffset 属性,以使大头针按您想要的方式精确排列。

    关于标注的自定义,最简单的方法是指定leftCalloutAccessoryViewrightCalloutAccessoryView 和/或detailCalloutAccessoryView。这为您提供了令人惊讶的控制程度,可以添加各种图像、标签等。

    如果您想彻底重新设计标注,可以让 viewForAnnotationcanShowCallout 设置为 NO,然后在自定义注释视图中回复 setSelected 以显示您自己的标注。在 Swift 中,请参阅 Customize MKAnnotation Callout View? 了解一些自定义标注的选项。

    【讨论】:

    • @Abhishek iOS 中没有针对 iPhone 大小的设备内置的标准弹出框控件。您必须使用第三方库。如果你谷歌或搜索 S.O.对于“iphone popover”,你会看到很多选项。
    • 这是假设您想要转换到另一个视图控制器,传递您刚刚选择的注释的详细信息。因此,要做到这一点,该目的地需要一些公共属性来指定您选择的注释。这行代码所做的就是在该目标视图控制器中设置该公共属性。这就是您在视图控制器之间传递信息的方式。
    • @Rob- 谢谢,我遵循了这个方法并且一切正常,除了添加自定义注释时,didSelectAnnotationView 会自动调用。在这里检查我的代码:stackoverflow.com/questions/33297059/…。我哪里错了?
    猜你喜欢
    • 2011-12-22
    • 2016-02-24
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    相关资源
    最近更新 更多