gpt4 book ai didi

ios - 如何为 MapView 上的每个自定义注释添加图像?

转载 作者:行者123 更新时间:2023-11-29 10:37:28 24 4
gpt4 key购买 nike

我有一类 Picture,其中包含每个位置的 latitudelongitude 以及 image。我想将它们作为注释添加到 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];

我是否还需要委托(delegate)并且必须调用“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

位置的数量是可变的,也是动态的。

如何将该图像传递给委托(delegate)人。我试图在传递的注释中找到图像字段,但它不存在!我很感激任何建议。

最佳答案

您需要将注解转换到您的自定义类,以便您可以访问它的属性 -

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

// Handle any custom annotations.
if ([annotation isKindOfClass:[Annotation class]])
{
Annotation *myAnn=(Annotation *)annotation;
// 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.calloutOffset = CGPointMake(0, 4);

} else {
pinView.annotation = annotation;
}
pinView.image = myAnn.image;
return pinView;
}
return nil;
}

关于ios - 如何为 MapView 上的每个自定义注释添加图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26192186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com