如何为 MapView 上的每个自定义注释添加图像? [英] How to add an image for each customized annotation on MapView?

查看:32
本文介绍了如何为 MapView 上的每个自定义注释添加图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Picture 类,它包含每个位置的 latitudelongitudeimage.我想将它们作为注释添加到 MapView 并显示其图像而不是这些位置的图钉.

I have a class of Picture which contains latitude and longitude and image of each location. I want to add them as annotation to the MapView and show its image instead of pin for those locations.

我有一个自定义的注释:

I have a customized annotation which is:

@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

现在在主代码中我正在尝试这个:

Now in main code I am trying this:

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"?因为它不起作用,我为委托做了这个:

Do I still need the delegate and must call "viewForAnnotation"? Because it did not work, I did this for delegate:

- (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.

This is working fine but adding same image to all the locations. But I have a specific image to show instead of image.png above for each location.

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

Number of location is variable and also dynamic.

如何将图像传递给委托.我试图在传递的注释中找到图像字段,但它不存在!我感谢任何建议.

How can I pass that image to the delegate. I tried to find the image field in the passed annotation, but it does not exist! I appreciate any suggestion.

推荐答案

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

You need to cast the annotation to your custom class so that you can access its properties -

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

这篇关于如何为 MapView 上的每个自定义注释添加图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆