starwar2012 2013-01-16 03:42 采纳率: 0%
浏览 4650
已采纳

在iphone中添加自定义AnnotationView

我用下面的代码制作自定义AnnotationView

我的CustomPointAnnotation是MKPointAnnotation 的子类:

   -(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。不知道怎么解决

  • 写回答

1条回答 默认 最新

  • lanhailvcao 2013-01-16 05:12
    关注

    不清楚你怎么声明类的,不过应该是这样的

    .h:

    @interface CustomPointAnnotation: NSObject <MKAnnotation> {
    
    }
    
    @property (nonatomic) CLLocationCoordinate coordinate;
    // Other properties like title, subtitle, etc.
    

    .m

    @implementation CustomPointAnnotation 
    
    @synthesize coordinate; // and other properties
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算