用于从URL进行注释的MapBox图像 [英] MapBox image for annotation from url

查看:181
本文介绍了用于从URL进行注释的MapBox图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using MapBox, and right now, I'm experiencing next problem: I'm using MapBox's delegate method for implementing image for annotation. Now I have some annotation images that needs to be loaded from URL. Problem is that method for custom image is called before image is loaded from URL and there is not image shown on Map. This is code in method:

- (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"];
}

推荐答案

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

Two ways to load network resources and use them as annotation images:

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

  1. Use a placeholder image and then update the annotation once the real image has loaded.

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

Updating the image is supported in Mapbox iOS SDK v3.1.0+; previously this required removing and re-adding the annotation.

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

Download the image before adding the annotation.

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

Additionally, the code you included does not check if the annotation image can be dequeued and reused — it always creates a new annotation image. The reuse pattern should look more 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;
}

这篇关于用于从URL进行注释的MapBox图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆