我有这个自定义 MKPinAnnotation,我需要向其中添加图像(如缩略图)。我也应该能够通过点击它来全屏打开图像。这样做的最佳方法是什么?
问问题
554 次
1 回答
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
有关更多信息,请参阅位置感知编程指南中的注释地图。
于 2013-05-14T18:30:11.533 回答