设置MKAnnotation的图像

时间:2012-07-10 05:57:37

标签: iphone objective-c mkmapview mkannotation

我使用以下代码在mapview中添加了MKAnnotation。

Flag *flag = [[Flag alloc] init]; 
flag.title = @"Golf Course";
flag.subtitle = @"Green"; 
flag.coordinate =CLLocationCoordinate2DMake(28.457394,77.108667);
[mapView addAnnotation:flag];

并设置图像

 -(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
 (id <MKAnnotation>)annotation 
 {
   if(([annotation isKindOfClass:[Flag class]])) 
   {
      MKPinAnnotationView *pinView = nil; 
      if(annotation != mapView.userLocation) 
      {
        static NSString *defaultPinID = @"PinId1";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                          initWithAnnotation:annotation reuseIdentifier:@"pin1"] autorelease];

        //pinView= [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin1"];

        pinView.canShowCallout = YES;
               pinView.draggable = YES;
        [pinView setImage:[UIImage imageNamed:@"flag2.png"]];

     } 
     else 
     {
        [mapView.userLocation setTitle:@"I am here"];
     }

   }
    return pinView;
 }

但是我没有得到MKAnnoation的图片。我们做错了什么?请帮帮我。

2 个答案:

答案 0 :(得分:6)

我有同样的问题。现在使用下面给出的工作版本。

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    if ([annotation isKindOfClass:[Annotation class]])
    {
        static NSString* SFAnnotationIdentifier = @"SFAnnotationIdentifier";
        MKPinAnnotationView* pinView =
        (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];
        if (!pinView)
        {
            MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                             reuseIdentifier:SFAnnotationIdentifier] autorelease];
            annotationView.canShowCallout = YES;

            UIImage *flagImage = [UIImage imageNamed:@"map_pin.png"];

            CGRect resizeRect;

            resizeRect.size = flagImage.size;
            CGSize maxSize = CGRectInset(self.view.bounds,
                                         10.5,
                                         10.5).size;
            maxSize.height -= self.navigationController.navigationBar.frame.size.height + 40.0;
            if (resizeRect.size.width > maxSize.width)
                resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
            if (resizeRect.size.height > maxSize.height)
                resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);

            resizeRect.origin = (CGPoint){0.0f, 0.0f};
            UIGraphicsBeginImageContext(resizeRect.size);
            [flagImage drawInRect:resizeRect];
            UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            annotationView.image = resizedImage;
            annotationView.opaque = NO;

            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

            Annotation *annoterObject = (Annotation *) annotation;

            rightButton.tag = annoterObject.annoteCount;

            [rightButton addTarget:self
                            action:@selector(showDetails:)
                  forControlEvents:UIControlEventTouchUpInside];
            annotationView.rightCalloutAccessoryView = rightButton;

            return annotationView;
        }
        else
        {
            pinView.annotation = annotation;
        }
        return pinView;
    }

    return nil;

}

这是工作代码。

答案 1 :(得分:0)

你必须在哪里设置图像?是否在标注上?如果是,则将[pinView setImage:[UIImage imageNamed:@"flag2.png"]];替换为pinView.leftCalloutAccessoryView = [[ UIImageView alloc]initWithImage:[UIImage imageNamed:@"flag2.png"]];