我有这个自定义MKPinAnnotation我需要添加图像(像缩略图)。我也应该能够通过点击它来打开全屏图像。这样做的最佳方法是什么?
答案 0 :(得分:1)
有几点想法。
如果您不想在地图上添加图钉,而是需要一些自定义图像,则可以设置地图的委托,然后编写一个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
}
如果您真的想要通过其公开按钮绕过标注,而是在点击注释视图时直接转到另一个视图控制器,您会:
在canShowCallout
中将NO
设为viewForAnnotation
;以及
有关详细信息,请参阅位置感知编程指南中的Annotating Maps。