用于注释的 MapBox 图片来自 url
MapBox image for annotation from url
我正在使用 MapBox,现在,我遇到了下一个问题:我正在使用 MapBox 的委托方法来实现用于注释的图像。现在我有一些注释图像需要从 URL 加载。问题是在从 URL 加载图像之前调用了自定义图像的方法,并且地图上没有显示图像。这是方法中的代码:
- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation
{
MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:@"custom"];
NSURL *url = [NSURL URLWithString:@"http://www.fnordware.com/superpng/pnggrad16rgb.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
annotationImage = [MGLAnnotationImage annotationImageWithImage:image reuseIdentifier:@"customUrl"];
}
加载网络资源并将其用作注释图像的两种方式:
使用占位符图像,然后在加载真实图像后更新注释。
Mapbox支持更新图片iOS SDK v3.1.0+;以前这需要删除并重新添加注释。
在添加注释之前下载图像。
此外,您包含的代码不会检查注释图像是否可以出列和重复使用——它总是创建一个新的注释图像。重用模式应该看起来更 like this:
- (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;
}
我正在使用 MapBox,现在,我遇到了下一个问题:我正在使用 MapBox 的委托方法来实现用于注释的图像。现在我有一些注释图像需要从 URL 加载。问题是在从 URL 加载图像之前调用了自定义图像的方法,并且地图上没有显示图像。这是方法中的代码:
- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation
{
MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:@"custom"];
NSURL *url = [NSURL URLWithString:@"http://www.fnordware.com/superpng/pnggrad16rgb.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
annotationImage = [MGLAnnotationImage annotationImageWithImage:image reuseIdentifier:@"customUrl"];
}
加载网络资源并将其用作注释图像的两种方式:
使用占位符图像,然后在加载真实图像后更新注释。
Mapbox支持更新图片iOS SDK v3.1.0+;以前这需要删除并重新添加注释。
在添加注释之前下载图像。
此外,您包含的代码不会检查注释图像是否可以出列和重复使用——它总是创建一个新的注释图像。重用模式应该看起来更 like this:
- (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;
}