如何创建自定义MKAnnotationView和自定义注释标题和副标题

Abh*_*hek 18 map mkannotation mkannotationview ios iphone-5

在此输入图像描述

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

Rob*_*Rob 37

要创建自定义的注解视图(您更换为标准的引脚),你可以设置image的属性MKAnnotationViewviewForAnnotation方法:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

您可能还需要调整centerOffset属性以使引脚按照您希望的方式排列.

关于标注的定制,最简单的方法是指定leftCalloutAccessoryView,rightCalloutAccessoryView和/或detailCalloutAccessoryView.这为您提供了惊人的控制程度,添加了各种图像,标签等.

如果您想对标注进行彻底的重新设计,您可以viewForAnnotation设置canShowCalloutNO然后setSelected在自定义注释视图中进行响应以显示您自己的标注.在Swift中,请参阅自定义MKAnnotation标注视图?有一些用于自定义标注的选项.