在iphone中添加自定义AnnotationView

时间:2013-01-15 06:26:25

标签: iphone ios objective-c cocoa-touch

我正在使用以下代码来制作自定义AnnotationView。

我的CustomPointAnnotationMKPointAnnotation的子类,其setter / getter为Player

   -(void) showMarkers
    {
        [self.mapView removeAnnotations:[self.mapView annotations]];
        for (int i = 0 ; i < [self.playersArray count];  i ++)
        {
            Players *player = [self.playersArray objectAtIndex:i];

            CustomPointAnnotation *annotationPoint = [[CustomPointAnnotation alloc] init];
            [annotationPoint setPlayer:player];

            if ([player.name isEqualToString:self.name.text])
            {
                NSLog(@"%@ , %@" , player.name,self.name.text);
                annotationPoint.coordinate = CLLocationCoordinate2DMake([player.latitude doubleValue]+.1, [player.longitude doubleValue]+.1);
                  [self.mapView addAnnotation:annotationPoint];
            }
            else
            {
                NSLog(@"%@ , %@" , player.name,self.name.text);
                annotationPoint.coordinate = CLLocationCoordinate2DMake([player.latitude doubleValue], [player.longitude doubleValue]);
                [self.mapView addAnnotation:annotationPoint];
            }
         }
        [self.mapView setUserInteractionEnabled:YES];

    }

    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
      //  self.mapView.centerCoordinate = userLocation.location.coordinate;
        myLocation = userLocation;
    }

    -(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
        if ([[annotation title] isEqualToString:@"Current Location"] )
        {
               return nil;
        }


        CustomAnnotationView *annView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"players"];
        CustomPointAnnotation *ptAnnotation = (CustomPointAnnotation *) annotation;
        [annView setPlayer:ptAnnotation.player];

        annView.image = [UIImage imageNamed:@"marker.png"];
        annView.enabled = YES;
        [annView setUserInteractionEnabled:YES];

        return annView;

    }

问题出现在[annView setPlayer:ptAnnotation.player]; Unrecognized selector sent to instance 我正在添加CustomPointAnnotation,所以它应该将其转换回来。我该如何解决这个问题

2 个答案:

答案 0 :(得分:1)

好的,我不确定您是如何声明自定义类的,但它是这样的:

·H:

@interface CustomPointAnnotation: NSObject <MKAnnotation> {

}

@property (nonatomic) CLLocationCoordinate coordinate;
// Other properties like title, subtitle, etc.

的.m

@implementation CustomPointAnnotation 

@synthesize coordinate; // and other properties

答案 1 :(得分:0)

@interface CustomPointAnnotation : NSObject <MKAnnotation>
{

    Player *player;
}
@property(nonatomic,retain) Player *player;
@property (nonatomic) CLLocationCoordinate coordinate;

不要将MKAnnotation子类确认为MKAnnotation protocal。

@interface CustomAnnotationView : MKAnnotationView
{


}

无需设置播放器。当您需要访问播放器对象时,使用CustomAnnotationView中的注释来访问它。

CustomPointAnnotation *annotation=self.annotation;
annotation.player;