1

我正在尝试根据点名称在地图上显示两个图像。

@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];

viewFor注解代码:

-(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 的 name 成员来决定点和基于点的图像。例如 if([((MyAnnotationClass)annotation).name isEqualToString: @"1"])

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

请帮忙。

4

1 回答 1

3

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

于 2012-05-17T10:00:07.337 回答