是否可以基于 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;
}
然而它的结果是这样的:
有没有人做过这个或者对如何完成这个有好的想法?
最佳答案
使用 MKAnnotationView
而不是 MKPinAnnotationView
。替换它
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
replace use MKAnnotationView here
编辑:一种解决方案是使用自定义
动画
。
引用MKAnnotationView Animation example 我如何使用自定义动画删除 MKAnnotationViews 的示例。
关于objective-c - 通过 url 设置 MKPinAnnotationView 图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13449919/