[英]MKMarkerAnnotationView Doesn't Show
设置带有附近咖啡馆注释的地图视图。 但是,当我尝试使用MKMarkerAnnotationView
定制注释时,地图视图上没有任何显示。 当我记录标记时,帧是(0 0; 0 0);
我也尝试过pin,但还是没用。
我已经在分镜脚本上设置了代表。
我还调试了视图控制器,其中有标记视图,但由于宽度和高度为零,因此未显示它们?
- (void)viewDidLoad {
[super viewDidLoad];
[self.cafeMap setShowsUserLocation:YES];
[self.locationManager requestWhenInUseAuthorization];
self.cafes = [NSMutableArray array];
[self fetchData];
[self.cafeMap registerClass:[MKAnnotationView class] forAnnotationViewWithReuseIdentifier:@"marker"];
}
-(void)fetchData{
[self.networkManager fetchCafeData:^(NSArray * _Nonnull businesses) {
for (NSDictionary* cafeInfo in businesses) {
Cafe *cafe = [[Cafe alloc]initWithCafeInfo:cafeInfo];
[self.cafes addObject:cafe];
}
for (Cafe *cafe in self.cafes) {
[self.cafeMap addAnnotation:cafe];
}
[self.cafeMap showAnnotations:self.cafes animated:YES];
}];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
MKMarkerAnnotationView *marker = [[MKMarkerAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"marker"];
marker = (MKMarkerAnnotationView*) [mapView dequeueReusableAnnotationViewWithIdentifier:@"marker" forAnnotation:annotation];
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
marker.rightCalloutAccessoryView = button;
[marker setEnabled:YES];
[marker setCanShowCallout:YES];
NSLog(@"MARKER:%@",marker);
return marker;
}
这是输出:
标记:<MKAnnotationView:0x7f9fa3f333b0; 框架=(0 0; 0 0); 层= <CALayer:0x60000253d220 >>
请注意,您的消息说这是MKAnnotationView
。 那是因为你已经注册MKAnnotationView
您的标识符,而不是MKMarkerAnnotationView
。 你要:
[self.cafeMap registerClass:[MKMarkerAnnotationView class] forAnnotationViewWithReuseIdentifier:@"marker"];
viewForAnnotation
,您应该能够将viewForAnnotation
简化为:
MKAnnotationView *marker = [mapView dequeueReusableAnnotationViewWithIdentifier:@"marker" forAnnotation:annotation];
我个人将注释视图的配置移到其自己的子类中:
static NSString * const cafeClusteringIdentifier = @"cafe";
@interface CafeAnnotationView: MKMarkerAnnotationView
@end
@implementation CafeAnnotationView
- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
self.rightCalloutAccessoryView = button;
self.canShowCallout = true;
self.clusteringIdentifier = cafeClusteringIdentifier;
}
return self;
}
- (void)setAnnotation:(id<MKAnnotation>)annotation {
[super setAnnotation:annotation];
self.clusteringIdentifier = cafeClusteringIdentifier;
}
@end
通过这样做,避免了使用用于配置注释视图的代码使视图控制器膨胀。
注意,我正在设置clusteringIdentifier
以便您享受MKMarkerAnnotationView
行为。
然后我为MKMapViewDefaultAnnotationViewReuseIdentifier
注册该类:
[self.cafeMap registerClass:[CafeAnnotationView class] forAnnotationViewWithReuseIdentifier:MKMapViewDefaultAnnotationViewReuseIdentifier];
使用MKMapViewDefaultAnnotationViewReuseIdentifier
的好处是您根本不需要实现viewForAnnotation
。 完全删除该方法的实现。 在iOS 11及更高版本中,仅当需要执行一些特殊操作(例如为多种类型的注释具有多个自定义重用标识符)时,才需要实现viewForAnnotation
。
无论如何,这会产生:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.