【发布时间】:2013-04-21 14:11:44
【问题描述】:
我需要在 MKMapView 上创建上面的 Annotation 视图。我能够创建自定义注释视图,但是在点击注释时,视图需要打开带有大文本的图像,我无法创建那个。请提供一些链接或完成此任务的方法。
【问题讨论】:
标签: ios map mkannotation iphone-5 mkannotationview
我需要在 MKMapView 上创建上面的 Annotation 视图。我能够创建自定义注释视图,但是在点击注释时,视图需要打开带有大文本的图像,我无法创建那个。请提供一些链接或完成此任务的方法。
【问题讨论】:
标签: ios map mkannotation iphone-5 mkannotationview
要创建自定义注释视图(替换标准引脚),您只需在viewForAnnotation 方法中设置MKAnnotationView 的image 属性:
- (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 属性,以使大头针按您想要的方式精确排列。
关于标注的自定义,最简单的方法是指定leftCalloutAccessoryView、rightCalloutAccessoryView 和/或detailCalloutAccessoryView。这为您提供了令人惊讶的控制程度,可以添加各种图像、标签等。
如果您想彻底重新设计标注,可以让 viewForAnnotation 将 canShowCallout 设置为 NO,然后在自定义注释视图中回复 setSelected 以显示您自己的标注。在 Swift 中,请参阅 Customize MKAnnotation Callout View? 了解一些自定义标注的选项。
【讨论】:
didSelectAnnotationView 会自动调用。在这里检查我的代码:stackoverflow.com/questions/33297059/…。我哪里错了?