0

在我的应用程序中,我在地图上显示了多个注释。我有很多不同的注释,但基本上我有 12 种不同类型的注释,每个注释都有自己的图像(所以 12 个不同的注释图像)。

现在在我当前的代码中,我将 MKAnnotation 子类化并给它一个我称为“标识符”的属性,当我创建一个注释时,这个标识符将被设置为适当的值,所以稍后我可以过滤一组特定的注释并给他们正确的viewForAnnotation 中的图像。

- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation {
CustomAnnotation *pin = annotation;
MKAnnotationView *pinView=nil;

if ([annotation isKindOfClass:[MKUserLocation class]]) {

    return nil;
}
else if ([pin.identifier isEqualToString:@"tree"]) { //give annotation a tree image
    pinView = (MKPinAnnotationView *)[aMapView dequeueReusableAnnotationViewWithIdentifier:@"OwnerAnnotationString"];
    pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"OwnerAnnotationString"];
    pinView.image = [UIImage imageNamedForDevice:@"tree"];
    pinView.centerOffset = CGPointMake(0, -53/2);
    return pinView;
}
else if ([pin.identifier isEqualToString:@"house"]){ //give annotation a house image
    pinView = (MKPinAnnotationView *)[aMapView dequeueReusableAnnotationViewWithIdentifier:@"SmallGoldPinStringAnkeren"];
    pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"anotheridentifier"];
    pinView.image = [UIImage imageNamedForDevice:@"house"];
    pinView.centerOffset = CGPointMake(8, -43/2);

    return pinView;
}
//and so on

如果过滤过程确实效率低下,我不喜欢这样,所以我想知道最好和最快的方法是为不同的注释设置图像。

最后这个例子是用注释完成的,但我现在已经多次遇到这种情况,所以实际上我的问题是:

检查您正在与之交互的对象类型的最佳方法是什么,以便您可以在 Objective-C 中为该特定对象设置适当的值。

4

1 回答 1

0

不要给每个注释一个标识符,而是将它们放在一个 NSmutableArray 中,然后将该数组放在一个 NS(Mutable)Dictionary 中。让该字典中的键成为您现在拥有的标识符。这样,注释在创建时就已经排序,如果您需要它们,在一个数组中,您可以简单地创建一个新的可变数组并将字典中数组中的所有对象添加到其中。

于 2013-09-09T15:09:19.100 回答