单击时,自定义annotationView图像将恢复为引脚

时间:2010-04-04 23:08:32

标签: iphone iphone-sdk-3.0 mapkit

我正在使用下面的代码在地图上显示自定义图像(而不是默认引脚)。但是,当我点击某个项目(并显示标注)时,图像将恢复为默认的红色图钉。即使显示标注,如何保留自定义图像?

- (MKAnnotationView *) mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinAnnotation = nil;

    if (annotation != mapView.userLocation) 
    {
        static NSString *pinID = @"mapPin";
        pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
        if (pinAnnotation == nil)
            pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID] autorelease];

        // Set the image
        pinAnnotation.image = [UIImage imageNamed:@"TestIcon.png"];

        // Set ability to show callout
        pinAnnotation.canShowCallout = YES;

        // Set up the disclosure button on the right side
        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinAnnotation.rightCalloutAccessoryView = infoButton;

        [pinID release];
    }

    return pinAnnotation;
    [pinAnnotation release];
}

2 个答案:

答案 0 :(得分:5)

我发现通过将pinAnnotation设为MKAnnotationView而不是MKPinAnnotationView,我得到了理想的结果。图像不再变成引脚

    static NSString *pinID = @"mapPin";
    pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
    if (pinAnnotation == nil)
        pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID] autorelease];

    // Set the image
    pinAnnotation.image = [UIImage imageNamed:@"TestIcon.png"];

答案 1 :(得分:0)

您需要使用子视图而不是图像属性。这段代码成功解决了我的问题:

    UIImage * image = [UIImage imageNamed:@"blue_pin.png"];
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
    [annView addSubview:imageView];