我需要将从 webService 获取的自定义图像设置为 mapPins。为此,我在方法中编写了一个特定的代码
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"viewForAnnotation Method");
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
NSString *annotationIdentifier = @"CustomViewAnnotation";
MKAnnotationView* annotationView = [objMapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if(!annotationView)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:annotationIdentifier];
}
//annotationView.canShowCallout= YES;
NSLog(@"Annotation Index = %lu",(unsigned long)[mapView.annotations indexOfObject:annotation]);
Annotations *myCustomPinAnnot = (Annotations*)annotation;
NSLog(@"Selected title = %@",myCustomPinAnnot.title);
if ([myCustomPinAnnot.title isEqualToString:@" TOWER PLACE"]) {
arrayFromArray = [[NSMutableArray alloc] init];
NSString *eff0 = [[allDataArray objectAtIndex:0] pinEfficiencyObjectClass];
NSString *eff1 = [[allDataArray objectAtIndex:1] pinEfficiencyObjectClass];
NSString *eff2 = [[allDataArray objectAtIndex:2] pinEfficiencyObjectClass];
NSString *eff3 = [[allDataArray objectAtIndex:3] pinEfficiencyObjectClass];
[self calculateEfficiency:eff0 andSecondEff:eff1 andThirdEff:eff2 andFourthEff:eff3];
[arrayFromArray addObject:[allDataArray objectAtIndex:2]];
[arrayFromArray addObject:[allDataArray objectAtIndex:1]];
[arrayFromArray addObject:[allDataArray objectAtIndex:0]];
[arrayFromArray addObject:[allDataArray objectAtIndex:3]];
joinString=[NSString stringWithFormat:@"%@%@%@",@"mais_",[self getColourCode:eff0 andSecondEff:eff1 andThirdEff:eff2 andFourthEff:eff3],@".png"];
NSLog(@"Join String = %@",joinString); //JoinString is the image fetched from web service
UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:joinString]];
[imgV setFrame:CGRectMake(0, 0, 40, 40)];
annotationView.image = imgV.image;
}
return annotationView;
}
我得到一个带有相应图像的 4 个引脚。
现在,当我单击特定的图钉时,我可以将数据带到下一个视图,但图像不同。也就是说,如果我单击标题为“abc”的图钉,我会在下一个屏幕上成功显示标题。但是,如果图钉上的图像是红色的,那么下一个视图上的图像就不同了。
只有第一个和最后一个引脚的图像在下一个视图中正确显示。
第二个它给了我第一个图像,第三个它给了我最后一个图钉的图像。
诡异的。
我的 didSelectAnnotationCode。
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
Annotations *annView = view.annotation;
NSLog(@"Pin title in callout = %@",annView.pinTitle);
objOnClickPinView = [[onClickPinView alloc]initWithNibName:@"onClickPinView" bundle:nil];
objOnClickPinView.pinTitleString = annView.pinTitle;
[self presentViewController:objOnClickPinView animated:YES completion:nil];
}
注意:我不需要显示 calloutView。我需要点击 mapPin 直接导航。