我正在使用旧的应用程序,需要在Mapview中进行更改。以前,我们需要在mapview上显示多个注释,每个图钉上都具有相同的图像,但是现在,我们必须在注释视图图钉上显示不同的图像以显示地址。我正在使用下面的代码来显示注释图钉,但它始终在注释图钉上显示相同的图像。
这是我的代码:
- (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;
}
您正在为每个注释遍历事件类型数组,大概总是以eventTypeArray
中最后一个图像的图像结尾。
相反,您希望“事件类型”是注释的属性。然后,在生成注释视图时,您可以查看注释的事件类型以了解要使用的图像。
所以,首先,您还没有这样做,您的注释具有eventType
属性:
typedef NS_ENUM(NSUInteger, EventType) {
EventTypeSmiley,
EventTypeDollar,
EventTypeDonation,
};
@interface EventAnnotation: MKPointAnnotation
@property (nonatomic) EventType eventType;
@end
@implementation EventAnnotation
@end
现在,在这种情况下,我正在为事件类型使用枚举,但是您可以使用任意类型。 (即使您坚持使用事件类型数组,我仍然会使用枚举来删除散布在整个代码中的隐含0/1/2值。)
然后,在向地图添加注释时,请使用此新注释类型,而不要使用MKPointAnnotation
:
EventAnnotation *eventAnnotation = [[EventAnnotation alloc] init];
eventAnnotation.coordinate = coordinate;
eventAnnotation.title = @"Fund raiser";
eventAnnotation.eventType = EventTypeDollar;
现在所有注释都为EventAnnotation
,则可以让viewForAnnotation
相应地执行操作:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
NSAssert([annotation isKindOfClass:[EventAnnotation class]], @"Was expecting event annotation”); // obviously, handle non-EventAnnotation annotations however you want, but I’m going to catch failures for now
static NSString *identifier = @"EventAnnotation";
MKAnnotationView *annotationView = [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.canShowCallout = YES;
} else {
annotationView.annotation = annotation; // don't forget this line!
}
EventAnnotation *eventAnnotation = (EventAnnotation *)annotation;
switch (eventAnnotation.eventType) {
case EventTypeSmiley:
annotationView.image = [UIImage imageNamed:@"smiley.png"];
break;
case EventTypeDollar:
annotationView.image = [UIImage imageNamed:@"dollar1.png"];
break;
case EventTypeDonation:
annotationView.image = [UIImage imageNamed:@"donation.png"];
break;
}
return annotationView;
}