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

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

问题描述

我需要在MKMapView上创建注释视图。我可以创建自定义注释视图,但是在注释点击视图需要使用该大文本打开图像,我无法创建该视图。请提供一些链接或执行此任务的方式。

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.

推荐答案

创建自定义注释视图(替换标准pin),您可以设置 image 属性的 MKAnnotationView viewForAnnotation 方法:

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.

关于标注的自定义,最简单的方法要指定 leftCalloutAccessoryView rightCalloutAccessoryView 和/或 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.

如果你想对标注进行彻底的重新设计,你可以有 viewForAnnotation set canShowCallout to 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天全站免登陆