代码之家  ›  专栏  ›  技术社区  ›  Danny Tuppeny

单击时,自定义注释视图图像将恢复为接点

  •  2
  • Danny Tuppeny  · 技术社区  · 14 年前

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

    - (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 回复  |  直到 14 年前
        1
  •  5
  •   Adam Searle    14 年前

    我发现通过将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"];
    
        2
  •  0
  •   Anthony Main    14 年前

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