1
我有一类Picture
其中包含latitude
和longitude
和image
的每个位置。我想将它们作为注释添加到MapView
,并显示其图像而不是针对这些位置的图钉。如何为MapView上的每个自定义注记添加图像?
我有一个自定义的注释是:
@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
。
位置数量是可变的,也是动态的。
如何将该图像传递给委托人。我试图在传递的注释中找到图像域,但它不存在!我赞赏任何建议。