MKAnnotation不会触发针点绘制方法

时间:2012-04-16 15:05:00

标签: ios map mkmapview mkannotation

我正在使用以下类创建带有Map和注释的视图:

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

@interface ContactDetailAnnotation : NSObject <MKAnnotation>

- (id) initWithLatitude:(CLLocationDegrees) lat longitude:(CLLocationDegrees) lng;
- (id) initWithCoordinate:(CLLocationCoordinate2D) coordinateArg;

@property (unsafe_unretained) CLLocationDegrees latitude;
@property (unsafe_unretained) CLLocationDegrees longitude;
@property (unsafe_unretained, nonatomic) CLLocationCoordinate2D coordinate;

@end

问题在于方法:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
用来绘制我的观点并不是被惹恼了。 如果我使用

self.myMap.showsUserLocation = TRUE;    

该方法是三角形的,所以问题必须在我创建的类中。

我的.m

#import "ContactDetailAnnotation.h"

@implementation ContactDetailAnnotation

@synthesize latitude = _latitude, longitude = _longitude, coordinate = _coordinate;

- (id) initWithCoordinate:(CLLocationCoordinate2D) coordinateArg
{
    self.coordinate = coordinateArg;

    return self;
}

- (id) initWithLatitude:(CLLocationDegrees)lat longitude:(CLLocationDegrees)lng 
{
    self.latitude = lat;
    self.longitude = lng;

    return self;
}

- (CLLocationCoordinate2D) coordinate 
{
CLLocationCoordinate2D coord = {self.latitude, 
                                self.longitude};
return coord;
}

@end

1 个答案:

答案 0 :(得分:0)

您是否已将注释添加到mapview的annotations数组?

修改

这是一个对我有用的简单实现:

MyAnnotation.h:

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

@interface MyAnnotation : NSObject <MKAnnotation>
{
    CLLocationCoordinate2D coordinate;
}

@property (nonatomic) CLLocationCoordinate2D coordinate;

- (id) initWithCoordinate:(CLLocationCoordinate2D)aCoordinate;

@end

MyAnnotation.m

#import "MyAnnotation.h"

@implementation MyAnnotation

@synthesize coordinate;

- (id) initWithCoordinate:(CLLocationCoordinate2D)aCoordinate
{
    coordinate = aCoordinate;
    return self;
}

@end

我的视图控制器:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CLLocationCoordinate2D coordinate = {42.149,-74.9384};
    MyAnnotation *myAnnotation = [[MyAnnotation alloc] initWithCoordinate:coordinate];
    [self.mapView addAnnotation:myAnnotation];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
{
    MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"Identifier"];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Identifier"];
    }
    return annotationView;
}