gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 04:22:37 27 4
gpt4 key购买 nike

我的 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 仍然是默认的,有人可以帮我解决这个问题吗?谢谢

最佳答案

您不应该自己调用 viewForAnnotation。您应该只向 map View 添加注释,然后它会确定在任何给定时刻哪些是可见的,并为您调用 viewForAnnotation。因此:

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

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

    @implementation MyAnnotation
    @end
  • 向您的 map View 添加注释(不是注释 View ),例如:

    - (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,我会使用驼峰命名法(以小写字母开头),我会将方法名称从 setAnnotationaddAnnotationWithTitle 等内容

  • 确保您的 Controller 已被指定为 mapView 的委托(delegate);

  • 将为您调用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;
    }

    请注意,animatesDrop 是一个 MKPinAnnotationView 选项,您无法使用自定义注释 View 。但如果您愿意,很容易实现它(请参阅 How can I create a custom "pin-drop" animation using MKAnnotationView? )。我建议您先解决基本的注释 View ,然后再看看自定义注释 View 放置动画。

关于ios - 为什么我的 MKPointAnnotation 不是自定义的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16507171/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com