将图像添加到MapViewAnnotation

Bra*_*don 2 iphone objective-c mapkit mkmapview ios

我正在尝试将图像添加到我的MKAnnotation中.我有下面的代码.我该怎么把@"map_icon.png"添加到这个?任何帮助都会很棒.谢谢!

mapView.m文件:

// retrieve latitude and longitude from the dictionary entry

location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];

// create the annotation

newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"name"]
                                               andCoordinate:location];
Run Code Online (Sandbox Code Playgroud)

MapViewAnnotation.m文件

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d 
{
title = ttl;
coordinate = c2d;
return self;
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

Rob*_*Rob 5

你通过回应来做到这一点mapView:viewForAnnotation,例如:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MapViewAnnotation class]])
    {
        MKAnnotationView* annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                        reuseIdentifier:@"MyCustomAnnotation"];
        annotationView.image = [UIImage imageNamed:@"map_icon.png"];
        annotationView.canShowCallout = YES;

        // If you've added the QuartzCore.framework to your project, you can also add a shadows/borders/etc.
        // For example, this code adds a shadow
        //            
        // [annotationView.layer setShadowColor:[UIColor blackColor].CGColor];
        // [annotationView.layer setShadowOpacity:0.8f];
        // [annotationView.layer setShadowRadius:5.0f];
        // [annotationView.layer setShadowOffset:CGSizeMake(0, 0)];
        // [annotationView setBackgroundColor:[UIColor whiteColor]];

        return annotationView;
    }

    // Note, if the annotation isn't your custom annotation (e.g. a `MKUserAnnotation`)
    // then return nil so default behavior will take place.

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

有关详细信息,请参阅" 位置感知编程指南"中的" 向地图添加注释"部分.