为什么我的MKPointAnnotation不是自定义的? [英] Why is my MKPointAnnotation not Custom?

查看:101
本文介绍了为什么我的MKPointAnnotation不是自定义的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MKPointAnnotation应该使用此代码进行自定义:

My MKPointAnnotation should with this code be custom:

-(MKPointAnnotation*)setAnnotation: (NSString*) title atLocation:(CLLocationCoordinate2D)Location withImage:(UIImage*) LocationImage{
    Pin = [[MKPointAnnotation alloc] init];
    Pin.title = title;
    [Pin setCoordinate:Location];
    [self mapView:mapView viewForAnnotation:Pin].annotation = Pin;
    return Pin;
}



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

        MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];


        if(!pinView){
            pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
            pinView.animatesDrop = YES;
            pinView.canShowCallout = YES;
            pinView.enabled = YES;
            UIButton *PicButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [PicButton addTarget:self action:@selector(showLocationPicture) forControlEvents:UIControlEventTouchUpInside];\
            pinView.rightCalloutAccessoryView = PicButton;
        }
        else{
            pinView.annotation = annotation;
        }
    return pinView;
}

然而,出于某种原因,Pin仍然是默认值,任何人都可以帮助我在这里?
谢谢

Yet, for some reason, the Pin is still default, can anyone help me out here? Thanks

推荐答案

你不应该自己打电话给 viewForAnnotation 。您只应在地图视图中添加注释,然后确定在任何给定时刻可见的注释,并为您调用 viewForAnnotation 。因此:

You shouldn't call viewForAnnotation yourself. You should only add annotations to your map view, which will then determine which are visible at any given moment and call viewForAnnotation for you. Thus:


  • 鉴于不同的注释可以有不同的图像,你真的想要子类 MKPointAnnotation 并给它一个 imageName 的属性(注意,不是 UIImage 本身,而是一个名字或 viewForAnnotation 的标识符将能够用于创建 UIImage 本身):

  • Given that different annotations can have different images, you really want to subclass MKPointAnnotation and give it a property of the imageName (note, not the UIImage itself, but a name or identifier that viewForAnnotation will be able to use to create the UIImage itself):

@interface MyAnnotation : MKPointAnnotation
@property(nonatomic, strong) NSString *imageName;
@end

@implementation MyAnnotation
@end


  • 在地图视图中添加注释(不是注释视图),例如:

  • Add an annotation (not an annotation view) to your mapview, e.g.:

    - (id<annotation>)addAnnotationWithTitle:(NSString *)title coordinate:(CLLocationCoordinate2D)coordinate imageName:(NSString *)imageName
    {
        MyAnnotation *annotation = [[MyAnnotation alloc] init];
        annotation.title = title;
        annotation.coordinate = coordinate;
        annotation.imageName = imageName;
        [mapView addAnnotation:annotation];
        return annotation;
    }
    

    注意,我不建议制作 Pin 类ivar / property,我使用camelCase作为变量名(以小写字母开头),我将方法名称从 setAnnotation 更改为类似 addAnnotationWithTitle 等。

    Note, I wouldn't suggest making Pin a class ivar/property, and I'd use camelCase for variable names (start with lowercase), I'd change the method name from setAnnotation to something like addAnnotationWithTitle, etc.

    确保您的控制器已被指定为mapView的委托;

    Make sure your controller has been specified as the delegate of the mapView;

    将为您调用 viewForAnnotation (如果注释可见)。它应该类似于:

    The viewForAnnotation will be called for you (if the annotation is visible). It should probably be something like:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
        if ([annotation isKindOfClass:[MKUserLocation class]])
            return nil;
    
        if ([annotation isKindOfClass:[MyAnnotation class]])
        {
            MyAnnotation *customAnnotation = annotation;
    
            static NSString *annotationIdentifier = @"MyAnnotation";
            MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
            if (!annotationView)
            {
                annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
                annotationView.canShowCallout = YES;
                annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            }
            else
            {
                annotationView.annotation = annotation;
            }
    
            annotationView.image = [UIImage imageNamed:customAnnotation.imageName];
    
            return annotationView;
        }
    
        return nil;
    }
    

    注意, animatesDrop 是一个 MKPinAnnotationView 选项,您无法使用自定义注释视图。但如果你愿意,可以很容易地实现一个(参见如何使用MKAnnotationView创建自定义pin-drop动画?)。我建议你先解决基本的注释视图,然后再看看自定义注释视图下拉动画。

    Note, the animatesDrop is a MKPinAnnotationView option that you can't do with custom annotation views. But it's easy to implement one if you want it (see How can I create a custom "pin-drop" animation using MKAnnotationView?). I'd suggest you tackle the basic annotation view first, and then look at custom annotation view drop animation later.

    这篇关于为什么我的MKPointAnnotation不是自定义的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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