我需要在MKMapView上创建上面的注释视图。我可以创建自定义注释视图,但在点击注释时,视图需要打开带有大文本的图像,我无法创建该视图。请给我提供一些链接或完成这项任务的方法。
发布于 2013-04-27 22:52:27
要创建自定义注释视图(标准管脚的替代品),只需在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?,了解一些用于自定义标注的选项。
https://stackoverflow.com/questions/16252764
复制相似问题