0

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

#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
4

1 回答 1

0

您是否将注释添加到地图视图的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

我的注释.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;
}
于 2012-04-16T20:19:55.713 回答