2

我正在尝试将图像添加到我的 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];

MapViewAnnotation.m 文件

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

谢谢!

4

2 回答 2

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

有关更多信息,请参阅位置感知编程指南的向地图添加注释部分。

于 2013-02-11T03:29:36.000 回答
2

在你的 mapView 控制器中实现 MKMapViewDelegate

myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
myMapView.delegate = self;

在您的 mapView 控制器中实现该方法

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

如果您想要自定义图像来替换默认图钉

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

if ([annotation isKindOfClass:[MapViewAnnotation class]])
{
    MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"];
    [annotationView setImage:[UIImage imageNamed:@"map_icon.png"]];
    return annotationView;
}
return nil;
}

如果您想要标注气泡中的图像

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
if ([annotation isKindOfClass:[MapViewAnnotation class]])
{
    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"];
    UIImageView *pin = [[UIImageView alloc] initWithFrame:CGRectMake(-5, -5, 10, 10)];
    [pin setImage:[UIImage imageNamed:@"map_icon.png"]];
    annotationView.leftCalloutAccessoryView = pin;
    return annotationView;
}
return nil;
}
于 2013-02-11T03:29:57.933 回答