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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R