我有一组图像,它们与 map 上的每个 Annotation
相关联。我可以向 leftCalloutAccessoryView
静态添加图像,但我不确定如何使其动态化。我希望它清楚我在问什么。每个注释都有自己想要显示的图像,但我不确定如何在以下方法中引用图像;
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
NSString *annotationIdentifier = @"PinViewAnnotation";
MyAnnotationView *pinView = (MyAnnotationView *) [mv dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!pinView)
{
pinView = [[MyAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
pinView.canShowCallout = YES;
UIImageView *houseIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Icon"]];//static image
[houseIconView setFrame:CGRectMake(0, 0, 30, 30)];
pinView.leftCalloutAccessoryView = houseIconView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
我的数组“self.sandwiches”包含具有名称 (NSString
) 和图像名称 ('NSString') 的 Sandwich
对象。
我正在寻找一种解决方案,我可以在其中获取所选引脚的索引,类似于 UITableView
,您可以在其中获取其索引,并使用 indexPath 从数组中访问它.行
.
我的注释类; 。H #进口 #进口 #导入
@interface SandwichAnnotation : NSObject<MKAnnotation>
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString * title;
@property (nonatomic,copy) NSString * subtitle;
@end
.m
#import "SandwichAnnotation.h"
@implementation SandwichAnnotation
@synthesize coordinate,title,subtitle;
@end
最佳答案
在viewForAnnotation
,而不是“获取 pin 的索引”(这可以工作但效率低于 UITableView
),我建议将所需的数据添加到注释类本身。
这样,数据更加独立,委托(delegate)方法或其他地方的代码无需担心、了解或与注释对象的位置或类型保持同步存储 中。只要您有对注释对象的引用,您将立即拥有该注释所需的所有数据(或者至少它本身将包含对相关数据的引用)。
viewForAnnotation
delegate 方法提供对它需要查看的注释对象的引用(annotation
参数)。一般输入为 id<MKAnnotation>
但它实际上是创建的确切类型的实例(由您创建 SandwichAnnotation
或由 map View 创建 MKUserLocation
)。
一种选择是使父级 Sandwich
类本身实现 MKAnnotation
并消除 SandwichAnnotation
类(class)。这样,自 annotation
以来根本不需要搜索或引用。参数实际上是一个Sandwich
.
但是,您可能希望为注释对象保留一个单独的类(这很好)。在这种情况下,您可以在注释类中添加对父对象的引用。示例:
@interface SandwichAnnotation : NSObject<MKAnnotation>
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString * title;
@property (nonatomic,copy) NSString * subtitle;
@property (nonatomic,retain) Sandwich * whichSandwich; // <-- add reference
@end
创建 SandwichAnnotation
时, 设置引用 Sandwich
注释用于:
for (Sandwich *currentSandwich in self.sandwiches) {
SandwichAnnotation *sa = [[SandwichAnnotation alloc] init...];
sa.coordinate = ...
sa.title = ...
sa.whichSandwich = currentSandwich; // <-- set reference
[mapView addAnnotation:sa];
}
最后,在 viewForAnnotation
, 如果 annotation
类型为 SandwichAnnotation
, 设置 leftCalloutAccessoryView
:
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation
{
if (! [annotation isKindOfClass:[SandwichAnnotation class]]) {
//If annotation is not a SandwichAnnotation, return default view...
//This includes MKUserLocation.
return nil;
}
//At this point, we know annotation is of type SandwichAnnotation.
//Cast it to that type so we can get at the custom properties.
SandwichAnnotation *sa = (SandwichAnnotation *)annotation;
NSString *annotationIdentifier = @"PinViewAnnotation";
MyAnnotationView *pinView = (MyAnnotationView *) [mv dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!pinView)
{
pinView = [[MyAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
pinView.canShowCallout = YES;
//Here, just initialize a blank UIImageView ready to use.
//Set image below AFTER we have a dequeued or new view ready.
UIImageView *houseIconView = [[UIImageView alloc] init];
[houseIconView setFrame:CGRectMake(0, 0, 30, 30)];
pinView.leftCalloutAccessoryView = houseIconView;
}
else
{
pinView.annotation = annotation;
}
//At this point, we have a dequeued or new view ready to use
//and pointing to the correct annotation.
//Update image on the leftCalloutAccessoryView here
//(not just when creating the view otherwise an annotation
//that gets a dequeued view will show an image of another annotation).
UIImageView *houseIconView = (UIImageView *)pinView.leftCalloutAccessoryView;
NSString *saImageName = sa.whichSandwich.imageName;
UIImage *houseIcon = [UIImage imageNamed: saImageName];
if (houseIcon == nil) {
//In case the image was not found,
//set houseIcon to some default image.
houseIcon = someDefaultImage;
}
houseIconView.image = houseIcon;
return pinView;
}
关于ios - 根据所选的 MKAnnotationView 动态更改 leftCalloutAccessoryView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25248405/