5

我的 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;
}

然而,由于某种原因,Pin 仍然是默认的,有人可以帮我吗?谢谢

4

1 回答 1

10

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

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

    @interface MyAnnotation : MKPointAnnotation
    @property(nonatomic, strong) NSString *imageName;
    @end
    
    @implementation MyAnnotation
    @end
    
  • 向您的地图视图添加注释(不是注释视图),例如:

    - (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 作为变量名(以小写开头),我会将方法名称从更改为setAnnotationaddAnnotationWithTitle

  • 确保您的控制器已被指定为 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;
    }
    

    请注意,这animatesDropMKPinAnnotationView您无法使用自定义注释视图执行的选项。但是,如果您愿意,它很容易实现(请参阅如何使用 MKAnnotationView 创建自定义“pin-drop”动画?)。我建议您先处理基本注释视图,然后再查看自定义注释视图放置动画。

于 2013-05-12T12:09:36.863 回答