iphone - 在 iPhone 中添加自定义 AnnotationView

标签 iphone ios objective-c cocoa-touch

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

我的CustomPointAnnotationMKPointAnnotation的子类,具有Player的setter/getter

   -(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];无法识别的选择器发送到实例 我正在添加 CustomPointAnnotation,因此它应该将其转换回来。我该如何解决这个问题

最佳答案

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

.h:

@interface CustomPointAnnotation: NSObject <MKAnnotation> {

}

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

.m

@implementation CustomPointAnnotation 

@synthesize coordinate; // and other properties

关于iphone - 在 iPhone 中添加自定义 AnnotationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14332219/

相关文章:

iphone - 适用于 ios 的 360° 全景库

objective-c - 不兼容的指针类型将 'NSString *__strong *' 发送到类型为 'NSError * _Nullable __autoreleasing * _Nullable' 的参数

iphone - CGRect var 作为属性值?

Linux、Windows 上的 iPhone 开发

ios - 避免在应用程序处于后台时使用位置

ios - 来自 Swift 的 POST 请求在 Postman 中工作时失败

c++ - react native 0.40.0 : Ordered comparison between pointer and zero ('NSNumber *' and 'int' )

objective-c - 提取字母的结构

ios - 是否有可能在设备上安装 iOS 应用程序后将资源加载到它?

ios - 我的 iOS 应用程序中的键盘在 iPhone 6 上太高了。如何在 XCode 中调整键盘的分辨率?