为什么我的MKPointAnnotation不是自定义的?

Bla*_*gic 5 objective-c ios mkmapviewdelegate

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

-(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;
}
Run Code Online (Sandbox Code Playgroud)

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

Rob*_*Rob 10

你不应该打电话给viewForAnnotation自己.您应该只在地图视图中添加注释,然后确定在任何给定时刻可以看到哪些注释并viewForAnnotation为您调用.从而:

  • 鉴于不同的注释可以有不同的图像,你真的想要子类MKPointAnnotation并赋予它一个属性imageName(注意,不是它UIImage自己,而是一个viewForAnnotation可以用来创建UIImage自身的名称或标识符):

    @interface MyAnnotation : MKPointAnnotation
    @property(nonatomic, strong) NSString *imageName;
    @end
    
    @implementation MyAnnotation
    @end
    
    Run Code Online (Sandbox Code Playgroud)
  • 向mapview添加注释(不是注释视图),例如:

    - (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;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    注意,我不建议创建Pin类ivar/property,并且我将camelCase用于变量名称(以小写开头),我将方法名称更改为setAnnotation类似addAnnotationWithTitle等等.

  • 确保已将控制器指定为mapView的委托;

  • viewForAnnotation会叫你(如果注释是可见的).它可能应该是这样的:

    - (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;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    请注意,这animatesDrop是一个MKPinAnnotationView您无法使用自定义注释视图的选项.但是如果你需要它很容易实现一个(请参阅如何使用MKAnnotationView创建自定义"pin-drop"动画?).我建议你首先解决基本的注释视图,然后再看看自定义注释视图下拉动画.