我试图在 map 上基于点名称显示两个图像。

@interface MyAnnotationClass : NSObject <MKAnnotation> {
    NSString *_name;
    NSString *_description;
    CLLocationCoordinate2D _coordinate;


}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

-(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate;

ViewDidLoad方法代码:
mapView.delegate = self;
    //Initialize annotation
    MyAnnotationClass *commuterLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake( 39.047752, -76.850388)];
    commuterLotAnnotation.name = @"1";
    MyAnnotationClass *overflowLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(  39.047958, -76.852520)];
    overflowLotAnnotation.name = @"2";

    //Add them to array
    self.myAnnotations=[NSArray arrayWithObjects:commuterLotAnnotation, overflowLotAnnotation, nil];

    //Release the annotations now that they've been added to the array
    [commuterLotAnnotation release];
    [overflowLotAnnotation release];

    //add array of annotations to map
    [mapView addAnnotations:_myAnnotations];

viewForAnnotation代码:
-(MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation{
    static NSString *parkingAnnotationIdentifier=@"ParkingAnnotationIdentifier";

    if([annotation isKindOfClass:[MyAnnotationClass class]]){

        //Try to get an unused annotation, similar to uitableviewcells
        MKAnnotationView *annotationView=[MapView dequeueReusableAnnotationViewWithIdentifier:parkingAnnotationIdentifier];
        //If one isn't available, create a new one
        if(!annotationView){
            annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:parkingAnnotationIdentifier];
           /* if(imgCount == 0){
                annotationView.image=[UIImage imageNamed:@"passenger.png"];
                imgCount = 1;
            }
            else if(imgCount == 1){
                annotationView.image=[UIImage imageNamed:@"place.png"];
                imgCount = 0;
            }*/
           // if([((MyAnnotationClass)annotation).name isEqualToString: @"1"])
            // code to show image
        }
        return annotationView;
    }
    return nil;
}

现在,我想在viewForAnnotation中访问MyAnnotationClass的名称成员,以基于点确定点和图像。例如
if([(((MyAnnotationClass)annotation).name isEqualToString:@“1”])

但它不起作用并且在((MyAnnotationClass)annotation)上显示异常

请帮忙。

最佳答案

([((MyAnnotationClass)annotation).name isEqualToString: @"1"])应该是([((MyAnnotationClass *)annotation).name isEqualToString: @"1"])。您需要将其强制转换为指向MyAnnotationClass的指针(*)。

09-07 14:08