【发布时间】:2013-05-14 17:33:30
【问题描述】:
我有这个自定义 MKPinAnnotation,我需要向其中添加图像(如缩略图)。我也应该能够通过点击它来全屏打开图像。这样做的最佳方法是什么?
【问题讨论】:
标签: ios uiimage mkpinannotationview
我有这个自定义 MKPinAnnotation,我需要向其中添加图像(如缩略图)。我也应该能够通过点击它来全屏打开图像。这样做的最佳方法是什么?
【问题讨论】:
标签: ios uiimage mkpinannotationview
一些想法。
如果您不想要地图上的图钉,而是一些自定义图像,您可以设置地图的委托,然后编写 viewForAnnotation 执行以下操作:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[CustomAnnotation class]])
{
static NSString * const identifier = @"MyCustomAnnotation";
// if you need to access your custom properties to your custom annotation, create a reference of the appropriate type:
CustomAnnotation *customAnnotation = annotation;
// try to dequeue an annotationView
MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView)
{
// if we found one, update its annotation appropriately
annotationView.annotation = annotation;
}
else
{
// otherwise, let's create one
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier];
annotationView.image = [UIImage imageNamed:@"myimage"];
// if you want a callout with a "disclosure" button on it
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// If you want, if you're using QuartzCore.framework, you can add
// visual flourishes to your annotation view:
//
// [annotationView.layer setShadowColor:[UIColor blackColor].CGColor];
// [annotationView.layer setShadowOpacity:1.0f];
// [annotationView.layer setShadowRadius:5.0f];
// [annotationView.layer setShadowOffset:CGSizeMake(0, 0)];
// [annotationView setBackgroundColor:[UIColor whiteColor]];
}
return annotationView;
}
return nil;
}
如果您使用标准标注(如上所示)执行此操作,则可以在用户点击标注的披露按钮时告诉地图您想要做什么:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
if (![view.annotation isKindOfClass:[CustomAnnotation class]])
return;
// do whatever you want to do to go to your next view
}
如果您真的想绕过带有其披露按钮的标注,而是在您点击注释视图时直接转到另一个视图控制器,您可以:
在您的viewForAnnotation 中将canShowCallout 设置为NO;和
有关详细信息,请参阅位置感知编程指南中的Annotating Maps。
【讨论】: