我正在处理旧应用程序,需要在 Mapview 中进行更改。以前我们需要在 mapview 上显示多个注释,每个图钉上都有相同的图像,但现在我们必须在注释 View 图钉上显示不同的图像才能显示地址。我正在使用以下代码来显示注释图钉,但它始终在注释图钉上显示相同的图像。
这是我的代码:
- (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
中最后一个关联的图像结束。
相反,您希望“事件类型”成为注释的属性。然后,在生成注释 View 时,您可以查看注释的事件类型以了解要使用的图像。
所以,首先,您还没有这样做,您将有一个具有 eventType
属性的注释:
typedef NS_ENUM(NSUInteger, EventType) {
EventTypeSmiley,
EventTypeDollar,
EventTypeDonation,
};
@interface EventAnnotation: MKPointAnnotation
@property (nonatomic) EventType eventType;
@end
@implementation EventAnnotation
@end
现在,在本例中,我为我的事件类型使用枚举,但您可以使用任何类型。 (即使您坚持使用事件类型数组,我仍然会使用枚举来删除散布在您的代码中的神秘 0/1/2 值。)
然后,当您向 map 添加注释时,使用这种新的注释类型,而不是 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;
}
关于ios - 在 ios Objective-c 的 mapview 上添加带有多个图像的多个注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61518006/