【发布时间】:2014-10-04 11:04:57
【问题描述】:
我有一个Picture 类,其中包含每个位置的latitude 和longitude 和image。我想将它们作为注释添加到 MapView 并显示其图像而不是这些位置的 pin。
我有一个自定义注释,它是:
@interface Annotation : MKAnnotationView <MKAnnotation>
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *subtitle;
@property (nonatomic,retain) UIImage *image;
@end
和
@implementation Annotation
@synthesize title,subtitle,coordinate,image;
@end
现在在主代码中我正在尝试这个:
CLLocationCoordinate2D location;
Annotation *myAnn;
for(Pictures *pic in picturesFromDB)
{
myAnn = [[Annotation alloc] init];
location.latitude = [pic.langT doubleValue];
location.longitude = [pic.longT doubleValue];
myAnn.coordinate = location;
myAnn.title = pic.descript;
myAnn.image = [UIImage imageWithData:pic.image]; //Not sure about this line!
[self.mapView addAnnotation:myAnn];
}
我仍然需要委托并且必须调用“viewForAnnotation”吗?因为它不起作用,所以我为代表做了这个:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
if ([annotation isKindOfClass:[Annotation class]])
{
// Try to dequeue an existing pin view first.
MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if (!pinView)
{
// If an existing pin view was not available, create one.
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
//pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"image.png"];
pinView.calloutOffset = CGPointMake(0, 4);
} else {
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
这工作正常,但将相同的图像添加到所有位置。但是对于每个位置,我要显示一个特定的图像,而不是上面的 image.png。
位置的数量是可变的,也是动态的。
如何将该图像传递给委托人。我试图在传递的注释中找到图像字段,但它不存在!我很感激任何建议。
【问题讨论】:
标签: ios xcode annotations mapkit mkannotationview