Setting image for MKAnnotation

点点圈 提交于 2019-12-06 11:51:52

I have the same issue. Now using the working version as given below.

- (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;

}

This is the working code.

Where you have to set image?is it on callouts? if yes replace [pinView setImage:[UIImage imageNamed:@"flag2.png"]]; with pinView.leftCalloutAccessoryView = [[ UIImageView alloc]initWithImage:[UIImage imageNamed:@"flag2.png"]];

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!