简体   繁体   English

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

[英]Android-Maps-Extensions load marker images asynchronously

I'm trying to show images (downloaded from a server) as map markers on a Google Map. 我正在尝试将图像(从服务器下载)显示为Google地图上的地图标记。 I'm using Android-Maps-Extensions because i wasn't happy with Google Map-Utils performance. 我正在使用Android-Maps-Extensions,因为我对Google Map-Utils的性能不满意。

Now i'm wondering how to load map marker images aynchronously with AME. 现在我想知道如何与AME异步加载地图标记图像。 The task is to show the image itself if it is not a cluster. 如果图像不是群集,则任务是显示图像本身。 If it is a cluster, i want to display the image of the first marker and the number of elements in the cluster. 如果它是一个集群,我想显示第一个标记的图像和集群中的元素数量。

Up to this point i've done the following: 到目前为止,我已经完成了以下工作:

1) Setup the Map Clusterer 1)设置Map Clusterer

private void setupMapClusterer() {
    mClusterOptions = new EClusterOptionsProvider(getContext());
    ClusteringSettings clusteringSettings = new ClusteringSettings();
    clusteringSettings.addMarkersDynamically(true);
    clusteringSettings.clusterOptionsProvider(mClusterOptions);
    mMap.setClustering(clusteringSettings);
}

2) Create the Cluster Options Provider 2)创建群集选项提供程序

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;
}

This gives me a Map where clusters have my custom view (which is correct), but i can't figure out where to download the images and how to put them into the single markers and especially the clusters. 这给了我一张地图,其中集群有我的自定义视图(这是正确的),但我无法弄清楚在哪里下载图像以及如何将它们放入单个标记,尤其是集群。

Of course i don't want to download all images in advance (we're talking about 500+) images. 当然我不想提前下载所有图像(我们说的是500多个)图像。

On a sidenode: I'm using Volley to dl the images asynchronously. 在sidenode上:我正在使用Volley来异步调整图像。

I created simple example of using clustering and loading markers async. 我创建了使用聚类和加载标记异步的简单示例。
Hope you find some useful here. 希望你在这里找到一些有用的。 https://github.com/pengrad/android-maps-async-markers https://github.com/pengrad/android-maps-async-markers

We will load icons with Glide, I found it more stable than Picasso. 我们将使用Glide加载图标,我发现它比毕加索更稳定。
Call loadIconMarker for every marker you are adding to the map. 为要添加到地图的每个标记调用loadIconMarker。

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);
        }
    });
}

Create ClusterIcon is a little harder. 创建ClusterIcon有点困难。 This is my ClusterOptionsProvider. 这是我的ClusterOptionsProvider。
I use base bitmap R.drawable.m1 and add text on it representing number of markers in cluster. 我使用基本位图R.drawable.m1并在其上添加表示集群中标记数量的文本。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM