0

地図ビューで場所をマークしようとしています。

まず、MKAnnotationこのようにプロトコルを別のクラスに実装しました。

AddressAnnotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface AddressAnnotation : NSObject <MKAnnotation>

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location;

@end

AddressAnnotation.m

#import "AddressAnnotation.h"

@implementation AddressAnnotation

- (id)initWithCoordinates:(CLLocationCoordinate2D)location
{
    self = [super init];
    if (self)
    {
        self.coordinate = location;
    }

    return self;
}

@end

次に、View Controller で、MKMapViewDelegate.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;
    }

    static NSString *myIdentifier = @"myIndentifier";
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:myIdentifier];

    if (!pinView)
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myIdentifier];
        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.animatesDrop = NO;
    }
    return pinView;
}

メソッドでは、クラスviewDidLoadのインスタンスを初期化します。AddressAnnotation

- (void)viewDidLoad
{
    [super viewDidLoad];

    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(34.421496, -119.70182);

    AddressAnnotation *pinAnnotation = [[AddressAnnotation alloc] initWithCoordinates:coordinate];
    [self.mapView addAnnotation:pinAnnotation];
}

ただし、次のエラーが発生し続けます。

-[AddressAnnotation setCoordinate:]: 認識されないセレクターがインスタンスに送信されました

ここで何が間違っているのかわかりません。誰かが私を助けてくれますか?

ありがとうございました。

4

2 に答える 2