程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-在iphone中添加自定義AnnotationView

ios-在iphone中添加自定義AnnotationView

編輯:編程綜合問答
在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。不知道怎麼解決

最佳回答:


不清楚你怎麼聲明類的,不過應該是這樣的

.h:

@interface CustomPointAnnotation: NSObject <MKAnnotation> {

}

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

.m

@implementation CustomPointAnnotation 

@synthesize coordinate; // and other properties
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved