首页 > 解决方案 > 在 ios Objective-c 中的 mapview 上添加多个带有多个图像的注释?

问题描述

我正在开发一个旧应用程序,需要在 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;
}

标签: iosobjective-cmkmapviewmkannotationmkannotationview

解决方案


您正在遍历每个注释的事件类型数组,大概总是以与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;
}

推荐阅读