【问题标题】:How to add an image for each customized annotation on MapView?如何为 MapView 上的每个自定义注释添加图像?
【发布时间】:2014-10-04 11:04:57
【问题描述】:

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


    【解决方案1】:

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

    - (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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      相关资源
      最近更新 更多