如何创建自定义 MKAnnotationView 和自定义注释标题和副标题 [英] How to create Custom MKAnnotationView and custom annotation title and subtitle

查看:42
本文介绍了如何创建自定义 MKAnnotationView 和自定义注释标题和副标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to create above Annotation view on MKMapView. I am able to create the custom annotation view but on the tap of annotation the view need to be opened image with that big text, I am not able to create that one. Please provide me some links or the way to do this task.

推荐答案

要创建自定义注释视图(您替换标准引脚),您只需设置 image MKAnnotationViewviewForAnnotation 方法:

To create a custom annotation view (your replacement for the standard pin), you can just set the image property of the MKAnnotationView in the viewForAnnotation method:

- (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 属性使图钉按照您想要的方式精确排列.

You might also want to adjust the centerOffset property to get the pin to line up precisely the way you want.

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

Regarding the customization of the callout, the easiest approach is to specify leftCalloutAccessoryView, rightCalloutAccessoryView and/or detailCalloutAccessoryView. This gives you a surprising degree of control, adding all sorts of images, labels, etc.

如果你想对标注进行彻底的重新设计,你可以让 viewForAnnotationcanShowCallout 设置为 NO 然后响应 setSelected 在您的自定义注释视图中显示您自己的标注.在 Swift 中,请参阅自定义 MKAnnotation 标注视图?,了解一些自定义选项标注.

If you want to do a radical redesign of the callout, you can have viewForAnnotation set canShowCallout to NO and then respond to setSelected in your custom annotation view to show your own callout. While in Swift, see Customize MKAnnotation Callout View? for a few options for customizing the callouts.

这篇关于如何创建自定义 MKAnnotationView 和自定义注释标题和副标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆