通过url设置MKPinAnnotationView的图像

时间:2012-11-19 08:17:43

标签: objective-c ios ios6 mapkit

是否可以基于URL以编程方式为MKPinAnnotationView设置图像?到目前为止,我有这个:

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

if ([annotation isKindOfClass:[infl8Node class]]) {
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.image = [UIImage imageNamed:@"invisible.png"];
    annotationView.animatesDrop = YES;

    //Add image from url
    NSURL *url = [NSURL URLWithString: @"http://cdn2.raywenderlich.com/downloads/arrest.png"];
    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
    [annotationView addSubview:imgView];

    return annotationView;
}
return nil;
}

然而它会产生如下结果: img on pin

有没有人这样做或者对如何做到这一点有好主意?

1 个答案:

答案 0 :(得分:1)

使用MKAnnotationView代替MKPinAnnotationView。替换它

 annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        replace use MKAnnotationView here

编辑:使用custom animation一个解决方案。

请参阅MKAnnotationView Animation example 我如何使用自定义动画删除MKAnnotationViews的示例。