Android Google Map自定义标记从URL加载图像

时间:2019-11-04 19:14:53

标签: android google-maps google-maps-markers google-maps-android-api-2

您好,我使用的是Google Map API v2,我已经创建了一个名为FirmaMarker的自定义标记类,我想将其与来自数据库的URL一起显示。
我使用Picasso加载图像,但是问题是标记是在获取图像之前创建的,因此我只是创建了没有图像的标记,并且用户需要重新打开活动几次,以便图像实际上被获取并显示。

这是自定义标记渲染器类

public class FirmaRenderer extends DefaultClusterRenderer<FirmaMarker> {

private final IconGenerator iconGenerator;
private final ImageView slika;
private final int width;
private final int height;
private Marker marker;
private MarkerOptions mOptions;
private Context context;

public FirmaRenderer(Context context, GoogleMap map, ClusterManager<FirmaMarker> clusterManager, int width, int height) {
    super(context, map, clusterManager);
    this.width = width;
    this.height = height;
    this.context = context;

    iconGenerator = new IconGenerator(context.getApplicationContext());
    slika = new ImageView(context.getApplicationContext());
    slika.setLayoutParams(new ViewGroup.LayoutParams(width,height));
    slika.setPadding(5,5,5,5);
    iconGenerator.setContentView(slika);
}
@Override
protected void onBeforeClusterItemRendered(FirmaMarker item, MarkerOptions markerOptions) {
    Picasso.get().load(item.getSlika()).fetch();
    Bitmap icon = iconGenerator.makeIcon();
    markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon)).title(item.getTitle());
    Picasso.get().load(item.getSlika()).into(slika);
}

@Override
protected boolean shouldRenderAsCluster(Cluster<FirmaMarker> cluster) {
    return false;
}}

我如何在主要活动中创建标记

        if(Map != null)
    {
        if(clusterManager == null)
        {
            clusterManager = new ClusterManager<FirmaMarker>(this,Map);
        }
        if(firmaRenderer == null)
        {
            firmaRenderer= new FirmaRenderer(
                    this,
                    Map,
                    clusterManager,
                    160,
                    100
            );
            clusterManager.setRenderer(firmaRenderer);
        }
        for(int i=0;i<FirmaID.size();i++)
        {
            FirmaMarker marker = new FirmaMarker(
                    new LatLng(Double.parseDouble(Sirina.get(i)), Double.parseDouble(Duzina.get(i))),
                    Naziv.get(i),
                    "Притисните за више информација",
                    LinkSlike.get(i),
                    Integer.parseInt(FirmaID.get(i))
            );
            clusterManager.addItem(marker);
            Markeri.add(marker);
        }
        clusterManager.cluster();
    }

0 个答案:

没有答案