2

我使用以下代码在我的地图视图中添加了 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 的图像。我在做什么错?请帮助我。

4

2 回答 2

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;

}

这是工作代码。

于 2012-07-10T06:05:51.783 回答
0

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

于 2012-07-10T06:51:45.513 回答