如何在MKMapView中的MKAnnotation上设置图像

Rak*_*mar 2 iphone ios

我正在开发一个用于聊天的应用程序,我必须在地图上显示所有朋友的图像.请提供实施指南.

我用过以下代码......

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
 {

MKPinAnnotationView *annView = [[MKPinAnnotationView alloc]init]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Done.png"]];

annView.animatesDrop = TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
[annView addSubview:imageView];
return annView;
 }
Run Code Online (Sandbox Code Playgroud)

谢谢

Nit*_*rad 12

在此输入图像描述

你做错了是你返回类MKPinAnnotationView的对象,它用于显示pin-annotation.

您应该使用MKAnnotationView类的对象.然后,通过更改其图像来自定义它:

annotationView.image = [UIImage imageNamed:@"friend_image.png"];
Run Code Online (Sandbox Code Playgroud)

现在,您可以获取朋友的照片而不是默认的PIN图像.

以下是解决您问题的完整代码:

  - (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
    {
        static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier";

        MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier];

        if (annotationView == nil)
        {
            annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier] autorelease];
        }

        // here you can assign your friend's image    
        annotationView.image = [UIImage imageNamed:@"friend_image.png"];
        annotationView.annotation = annotation;

        // add below line of code to enable selection on annotation view
        annotationView.canShowCallout = YES

        return annotationView;
    }
Run Code Online (Sandbox Code Playgroud)


Nit*_*rad 6

在此输入图像描述

我已经发布了一个带有不同样式注释的答案.但是,如果要在上图中显示注释,则必须使用MKAnnotationView类的leftCalloutAccessoryView属性,如下所示:

- >首先,创建pinAnnotation并将其添加到viewDidLoad中的 mapView 或创建mapView的位置:

    // Add the annotation to our map view
    MKPointAnnotation * pointAnnotation = [[MKPointAnnotation alloc] init];
    pointAnnotation.title = @"Rakesh Thummar";
    pointAnnotation.subtitle = @"Ahmedabad";
    pointAnnotation.coordinate = coord;
    [mapView addAnnotation:pointAnnotation];
Run Code Online (Sandbox Code Playgroud)

- >然后,使用MKAnnotationView类的leftCalloutAccessoryView属性:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier";

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier];

    if (annotationView == nil) {

        // if you want to show pinPoint create object of MKPinAnnotationView class instead of MKAnnotationView class
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier];
    }

    annotationView.canShowCallout = YES;

    // Add a custom image to the left side of the callout.
    UIImageView *myCustomImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo1_40.png"]];
    annotationView.leftCalloutAccessoryView = myCustomImage;

    return annotationView;
}
Run Code Online (Sandbox Code Playgroud)