首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android-Maps-Extensions异步加载标记图像

Android-Maps-Extensions异步加载标记图像
EN

Stack Overflow用户
提问于 2015-08-29 02:21:17
回答 1查看 5.8K关注 0票数 3

我正在尝试将图片(从服务器下载)显示为Google Map上的地图标记。我使用Android-Maps-Extensions是因为我对Google Map-Utils的性能不满意。

现在我想知道如何使用AME异步加载地图标记图像。如果图像不是集群,则任务是显示图像本身。如果它是一个集群,我想要显示第一个标记的图像和集群中元素的数量。

到目前为止,我已经完成了以下工作:

1)设置Map Clusterer

代码语言:javascript
复制
private void setupMapClusterer() {
    mClusterOptions = new EClusterOptionsProvider(getContext());
    ClusteringSettings clusteringSettings = new ClusteringSettings();
    clusteringSettings.addMarkersDynamically(true);
    clusteringSettings.clusterOptionsProvider(mClusterOptions);
    mMap.setClustering(clusteringSettings);
}

2)创建群集选项提供程序

代码语言:javascript
复制
public EClusterOptionsProvider(Context context) {
    mClusterOptions = new ClusterOptions();
    mContext = context;
    mIconGenerator = new IconGenerator(context);

    // Inflate Layout
    View markerView = LayoutInflater.from(context).inflate(R.layout.map_marker, null);
    mImageView = (ImageView) markerView.findViewById(R.id.map_marker_image);
    mTextView = (TextView) markerView.findViewById(R.id.map_marker_text);

    // Setup Icon Generator
    mIconGenerator.setContentView(markerView);
}

public Bitmap createIcon(Bitmap bmp, String text) {
    mImageView.setImageBitmap(bmp);
    mTextView.setText(text);
    return mIconGenerator.makeIcon();
}

@Override
public ClusterOptions getClusterOptions(List<Marker> list) {
    // Get Bitmap from first marker
    Marker first = list.get(0);
    mClusterOptions.icon(BitmapDescriptorFactory.fromBitmap(mIconGenerator.makeIcon()));
    return mClusterOptions;
}

这给了我一个地图,其中集群有我的自定义视图(这是正确的),但我不知道从哪里下载图像,以及如何将它们放入单个标记中,特别是集群中。

当然,我不想提前下载所有图像(我们谈论的是500+)图像。

在sidenode上:我使用Volley异步dl镜像。

EN

回答 1

Stack Overflow用户

发布于 2015-08-29 04:48:37

我创建了使用集群和异步加载标记的简单示例。

希望你能在这里找到一些有用的东西。https://github.com/pengrad/android-maps-async-markers

我们将使用Glide加载图标,我发现它比毕加索更稳定。

为要添加到地图中的每个标记调用loadIconMarker。

代码语言:javascript
复制
MarkerOptions markerOptions = ...
// you should pass some icon before new loaded, or leave default one
//markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.default_marker));
Marker marker = mMap.addMarker(markerOptions);
loadMarkerIcon(marker);


private void loadMarkerIcon(final Marker marker) {
    Glide.with(this).load("http://www.myiconfinder.com/uploads/iconsets/256-256-a5485b563efc4511e0cd8bd04ad0fe9e.png")
            .asBitmap().fitCenter().into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
            BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(bitmap);
            marker.setIcon(icon);
        }
    });
}

创建ClusterIcon要困难一些。这是我的ClusterOptionsProvider。

我使用基位图R.drawable.m1,并在其上添加文本,表示集群中的标记数量。

代码语言:javascript
复制
public class ClusterIconProvider implements ClusterOptionsProvider {

    Resources resources;
    Paint paint;
    Bitmap base;

    public ClusterIconProvider(Resources resources) {
        this.resources = resources;

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(15);

        base = BitmapFactory.decodeResource(resources, R.drawable.m1);
    }

    @Override
    public ClusterOptions getClusterOptions(List<Marker> list) {
        Bitmap bitmap = base.copy(Bitmap.Config.ARGB_8888, true);

        Rect bounds = new Rect();
        String text = String.valueOf(list.size());
        paint.getTextBounds(text, 0, text.length(), bounds);
        float x = bitmap.getWidth() / 2.0f;
        float y = (bitmap.getHeight() - bounds.height()) / 2.0f - bounds.top;

        Canvas canvas = new Canvas(bitmap);
        canvas.drawText(text, x, y, paint);
        BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(bitmap);

        return new ClusterOptions().anchor(0.5f, 0.5f).icon(icon);
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32277467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档