无法显示自定义MKAnnotationView标注

时间:2022-05-20 21:21:12

I'm building an OSX application that uses Mapkit, and I'm trying to get a callout to appear when I click on an MKAnnotationView on my map. To do this I'm implementing the MKMapViewDelegate, and the following function :

我正在构建一个使用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;
    }

The function runs fine everytime I put a new pin down, and I made sure the titles of the pins are not empty or null, but the callout still is not showing up. Anybody have any ideas?

每次我放下一个新引脚时,该函数运行正常,我确保引脚的标题不为空或为空,但标注仍未显示。有人有什么想法吗?

EDIT: After looking through Anna's response, I realized I misunderstood how to implement the MKAnnotation protocol. I've deleted my LocationPin class, which inherited from MKAnnotationView, and instead added the following class to represent my custom MKAnnotation, with a class inside of it to generate a custom MKAnnotationView:

编辑:在看完Anna的回复后,我意识到我误解了如何实现MKAnnotation协议。我删除了继承自MKAnnotationView的LocationPin类,而是添加了以下类来表示我的自定义MKAnnotation,其中包含一个类来生成自定义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

I've also changed the MKMapViewDelegate function the following way now:

我现在也通过以下方式更改了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;
}

But the callout still is not appearing. Any help is appreciated.

但标注仍然没有出现。任何帮助表示赞赏。

Edit 2: As requested, LocationAnnotation.h:

编辑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

1 个解决方案

#1


I figured out what was wrong. I was subclassing MKMapView, which is apparently not recommended by Apple, as it can cause some unwanted behavior.

我弄清楚出了什么问题。我是MKMapView的子类,显然不是Apple推荐的,因为它可能会导致一些不必要的行为。

#1


I figured out what was wrong. I was subclassing MKMapView, which is apparently not recommended by Apple, as it can cause some unwanted behavior.

我弄清楚出了什么问题。我是MKMapView的子类,显然不是Apple推荐的,因为它可能会导致一些不必要的行为。