如何从viewForAnnotation中识别XCode MKMapView引脚

时间:2016-05-07 20:59:35

标签: ios objective-c xcode mkmapview

我正在开发一个带有mkmapview的应用程序,它将引脚放到mapview上。

我需要能够根据有关引脚的信息为引脚着色。

删除地图引脚的当前代码是:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
if (annotation == self.mapView.userLocation) return nil;
NSLog(@"annotation = %@", annotation);
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* customPin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
customPin.pinColor = MKPinAnnotationColorRed;
customPin.animatesDrop = YES;
customPin.canShowCallout = YES;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
customPin.rightCalloutAccessoryView = rightButton;
return customPin;
}

如果我改变了行:

customPin.pinColor = MKPinAnnotationColorRed;

然后我可以改变所有掉落引脚的颜色,但是如何识别哪个引脚被丢弃,以便我只能在需要时对引脚进行重新着色?

我添加了日志行:

NSLog(@"annotation = %@", annotation);

但它会返回,例如:

annotation = <MapAnnotation: 0x7feabd749190>
annotation = <MapAnnotation: 0x7feac04edf50>
annotation = <MapAnnotation: 0x7feabd79f860>

如何使用它来识别引脚?

或者我应该在不同的位置着色注释引脚吗?

1 个答案:

答案 0 :(得分:0)

您可以将符合MKAnnotation协议的任何对象添加到地图中作为注释。

我建议创建一个具有额外属性的自定义注释对象(例如,针脚类型的枚举)

然后在viewForAnnotation方法中,一旦确定它不是用户位置注释,请将id指针强制转换为自定义注释对象类型并检查自定义属性以查看哪个类型的引脚为显示(它可以像switch语句一样简单。)