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

查看:91
本文介绍了如何在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天全站免登陆