无法显示自定义 MKAnnotationView 标注
Unable to get custom MKAnnotationView callout to appear
我正在构建一个使用 Mapkit 的 OSX 应用程序,并且我试图在我单击地图上的 MKAnnotationView 时显示标注。为此,我正在实施 MKMapViewDelegate 和以下函数:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[LocationPin class]]) {
LocationPin *returnPin = (LocationPin *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"LocationPin"];
if (!returnPin){
returnPin = [LocationPin createLocationPinForMapView:mapView annotation:annotation];
}
returnPin.title = annotation.title;
NSButton *rightButton = [[NSButton alloc] initWithFrame:NSMakeRect(0.0, 0.0, 100.0, 80.0)];
[rightButton setTitle:@"Info"];
[rightButton setBezelStyle:NSShadowlessSquareBezelStyle];
returnPin.annotation = annotation;
returnPin.canShowCallout = YES;
returnPin.rightCalloutAccessoryView = rightButton;
return returnPin;
}
return nil;
}
每次我放下一个新图钉时,该功能都运行良好,并且我确保图钉的标题不为空或为空,但标注仍然没有显示。有人有什么想法吗?
编辑:
看完 Anna 的回复后,我意识到我误解了如何实现 MKAnnotation 协议。我已经删除了我的 LocationPin class,它继承自 MKAnnotationView,而是添加了以下 class 来表示我的自定义 MKAnnotation,其中包含一个 class 以生成自定义 MKAnnotationView:
@implementation LocationAnnotation:
-(id)initWithTitle:(NSString *)newTitle Location:(CLLocationCoordinate2D)location {
self = [super init];
if(self) {
_title = newTitle;
_coordinate = location;
}
return self;
}
- (MKAnnotationView *)annotationView {
MKAnnotationView *annotationView = [[MKAnnotationView alloc]initWithAnnotation:self reuseIdentifier:@"LocAnno"];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [NSImage imageNamed:@"dvd"];
NSButton *rightButton = [[NSButton alloc] initWithFrame:NSMakeRect(0.0, 0.0, 100.0, 80.0)];
[rightButton setTitle:@"Info"];
[rightButton setBezelStyle:NSShadowlessSquareBezelStyle];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
}
@end
我现在还通过以下方式更改了 MKMapViewDelegate 函数:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[LocationAnnotation class]]) {
LocationAnnotation *anno = (LocationAnnotation *)annotation;
MKAnnotationView *returnPin = [mapView dequeueReusableAnnotationViewWithIdentifier:@"LocAnno"];
if (!returnPin){
returnPin = anno.annotationView;
}
else {
returnPin.annotation = annotation;
}
return returnPin;
}
return nil;
}
但是标注仍然没有出现。感谢任何帮助。
编辑 2:
根据要求,LocationAnnotation.h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface LocationAnnotation : NSObject <MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (copy, nonatomic) NSString *title;
-(id)initWithTitle:(NSString *)newTitle Location:(CLLocationCoordinate2D)location;
-(MKAnnotationView *)annotationView;
@end
我知道出了什么问题。我正在子类化 MKMapView,这显然不是 Apple 推荐的,因为它可能会导致一些不需要的行为。
我正在构建一个使用 Mapkit 的 OSX 应用程序,并且我试图在我单击地图上的 MKAnnotationView 时显示标注。为此,我正在实施 MKMapViewDelegate 和以下函数:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[LocationPin class]]) {
LocationPin *returnPin = (LocationPin *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"LocationPin"];
if (!returnPin){
returnPin = [LocationPin createLocationPinForMapView:mapView annotation:annotation];
}
returnPin.title = annotation.title;
NSButton *rightButton = [[NSButton alloc] initWithFrame:NSMakeRect(0.0, 0.0, 100.0, 80.0)];
[rightButton setTitle:@"Info"];
[rightButton setBezelStyle:NSShadowlessSquareBezelStyle];
returnPin.annotation = annotation;
returnPin.canShowCallout = YES;
returnPin.rightCalloutAccessoryView = rightButton;
return returnPin;
}
return nil;
}
每次我放下一个新图钉时,该功能都运行良好,并且我确保图钉的标题不为空或为空,但标注仍然没有显示。有人有什么想法吗?
编辑: 看完 Anna 的回复后,我意识到我误解了如何实现 MKAnnotation 协议。我已经删除了我的 LocationPin class,它继承自 MKAnnotationView,而是添加了以下 class 来表示我的自定义 MKAnnotation,其中包含一个 class 以生成自定义 MKAnnotationView:
@implementation LocationAnnotation:
-(id)initWithTitle:(NSString *)newTitle Location:(CLLocationCoordinate2D)location {
self = [super init];
if(self) {
_title = newTitle;
_coordinate = location;
}
return self;
}
- (MKAnnotationView *)annotationView {
MKAnnotationView *annotationView = [[MKAnnotationView alloc]initWithAnnotation:self reuseIdentifier:@"LocAnno"];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [NSImage imageNamed:@"dvd"];
NSButton *rightButton = [[NSButton alloc] initWithFrame:NSMakeRect(0.0, 0.0, 100.0, 80.0)];
[rightButton setTitle:@"Info"];
[rightButton setBezelStyle:NSShadowlessSquareBezelStyle];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
}
@end
我现在还通过以下方式更改了 MKMapViewDelegate 函数:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[LocationAnnotation class]]) {
LocationAnnotation *anno = (LocationAnnotation *)annotation;
MKAnnotationView *returnPin = [mapView dequeueReusableAnnotationViewWithIdentifier:@"LocAnno"];
if (!returnPin){
returnPin = anno.annotationView;
}
else {
returnPin.annotation = annotation;
}
return returnPin;
}
return nil;
}
但是标注仍然没有出现。感谢任何帮助。
编辑 2: 根据要求,LocationAnnotation.h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface LocationAnnotation : NSObject <MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (copy, nonatomic) NSString *title;
-(id)initWithTitle:(NSString *)newTitle Location:(CLLocationCoordinate2D)location;
-(MKAnnotationView *)annotationView;
@end
我知道出了什么问题。我正在子类化 MKMapView,这显然不是 Apple 推荐的,因为它可能会导致一些不需要的行为。