【发布时间】:2020-08-14 11:57:18
【问题描述】:
我正在开发一个旧应用程序,需要在 Mapview 中进行更改。以前我们需要在 mapview 上显示多个注释,每个 pin 上都有相同的图像,但现在我们必须在 annotation view pin 上显示不同的图像以显示地址。我正在使用以下代码来显示注释引脚,但它始终在注释引脚上显示相同的图像。
这是我的代码:
- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>) annotation
{
NSLog(@"Eventtype Array is %@",eventTypeArray);
MKAnnotationView * pinView = nil;
if(annotation != _mapvw.userLocation)
{
static NSString * defaultPinID = @"pinId";
pinView = (MKAnnotationView *)[_mapvw dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
{
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
}
for ( int i=0; i<[eventTypeArray count]; i++)
{
eventTypeStr = [NSString stringWithFormat:@"%@",
[eventTypeArray objectAtIndex:i]];
NSLog(@"Event Type is %@",eventTypeStr);
if ([eventTypeStr isEqualToString:@"0"])
{
NSLog(@"Eventtype Array is %@",eventTypeStr);
NSLog(@"Event Type is %@",eventTypeStr);
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"smiley.png"];
}
else if ([eventTypeStr isEqualToString:@"1"])
{
NSLog(@"Event Type is %@",eventTypeStr);
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"dollar1.png"];
}
else if ([eventTypeStr isEqualToString:@"2"])
{
NSLog(@"Event Type is %@",eventTypeStr);
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"donation.png"];
}
}
}
return pinView;
}
【问题讨论】:
-
在
if else之前完成for循环。附言此代码中还有许多其他可能的改进(例如使用switch大小写而不是if else,事件标识符wtc 的枚举/常量)。请考虑彻底审查和重构。
标签: ios objective-c mkmapview mkannotation mkannotationview