微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MKPointAnnotation 标题未显示且注释不可拖动

如何解决MKPointAnnotation 标题未显示且注释不可拖动

我认为这两个是相关的,我可能在这里做错了:

首先我要覆盖 viewForAnnotation - 为了放置我的自定义图像

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    // we avoid changing the user's location blue point
    if([annotation isKindOfClass: [MKUserLocation class]]) {
      return nil;
    }
    else
    {
    static Nsstring *SFAnnotationIdentifier = @"SFAnnotationIdentifier";
    MKPinAnnotationView* pinAnnotationView = (MKPinAnnotationView*)[self.mapToPin dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];

     if (!pinAnnotationView)
          {
              MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                 reuseIdentifier:SFAnnotationIdentifier];
                  UIImage *littleImage = [UIImage imageNamed:@"littleImage.png"];
                  // You may need to resize the image here.
                  annotationView.image = littleImage;
              
                  pinAnnotationView.draggable = true;
                  pinAnnotationView.canShowCallout = true;
                  pinAnnotationView.animatesDrop = true;
                  return annotationView;
          }
          else
          {
              pinAnnotationView.annotation = annotation;
          }
          return pinAnnotationView;
    }
    

}

pinAnnotationView 的 draggablecanShowCalloutanimatesDrop 似乎不起作用。

然后,我在 viewDidLoad 中调用我的方法来设置 Annotation

-(void) setAnnotation:(Nsstring*)lat : (Nsstring*)lng
{
    _mapAnnotation = [[MKPointAnnotation alloc] init];
    CLLocationCoordinate2D coordinateForPin;
    coordinateForPin.latitude = [lat floatValue];
    coordinateForPin.longitude = [lng floatValue];
    [_mapAnnotation setCoordinate:coordinateForPin];
    [_mapAnnotation setTitle:@"sometitle"];
    [_mapToPin addAnnotation:_mapAnnotation];
}

好像是[_mapAnnotation setTitle:@"sometitle"];当我覆盖 viewForAnnotation 时,它不再工作了。

这是正确的方法吗?我一直在浏览,但没有找到任何可以提供帮助的解决方案。任何反馈将不胜感激。

解决方法

问题是不小心引用了错误的变量名。 pinAnnotationView 是该 nil 语句中的 if,因此使用该 nil 引用设置属性是一个 NOOP。


if (!pinAnnotationView) {
    MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                    reuseIdentifier:SFAnnotationIdentifier];

    ...

    annotationView.draggable = true;      // not pinAnnotationView
    annotationView.canShowCallout = true;
    annotationView.animatesDrop = true;

    return annotationView;
}

FWIW,如果针对 iOS 10 或更高版本,您可以注册您自己的类并将您的注释视图配置代码移动到该子类中。此外,在 iOS 11 及更高版本中,您根本不需要 mapView:viewForAnnotation:

例如,在 iOS 11 及更高版本中:

//  MyAnnotationView.h

@import MapKit;

NS_ASSUME_NONNULL_BEGIN

@interface MyAnnotationView : MKPinAnnotationView

@end

NS_ASSUME_NONNULL_END

//  MyAnnotationView.m

#import "MyAnnotationView.h"

@implementation MyAnnotationView

- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

    if (self) {
        ...

        self.draggable = true;
        self.canShowCallout = true;
        self.animatesDrop = true;
    }

    return self;
}

@end

然后,在您的 viewDidLoad 中注册该课程。

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.mapView registerClass:[MyAnnotationView class] forAnnotationViewWithReuseIdentifier:MKMapViewDefaultAnnotationViewReuseIdentifier];
    ...
}

然后您可以完全删除 mapView:viewForAnnotation: 实现。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。