我试图在地图名称的基础上在地图上显示两个图像。
@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的name成员来决定点上的点和图像。例如 if([((MyAnnotationClass)annotation).name isEqualToString:@“1”])
但它不起作用并在((MyAnnotationClass)注释)上显示异常
请帮忙。
答案 0 :(得分:3)
此([((MyAnnotationClass)annotation).name isEqualToString: @"1"])
应为([((MyAnnotationClass *)annotation).name isEqualToString: @"1"])
。您需要将其强制转换为MyAnnotationClass的指针(*)。