带有毕加索的Google Maps Cluster项目标记图标 [英] Google Maps Cluster Item Marker Icon with Picasso
问题描述
我将Google Map SDK 7.3.0与android-maps-utils 0.3.4结合使用,因为我需要在地图上为我的标记创建集群.
I'm using Google Map SDK 7.3.0 with android-maps-utils 0.3.4 because I need clusters for my Markers on the map.
好,所以这里的问题是,我不应该有红色标记.仅绿色+蓝色标记.
我将DefaultClusterRenderer
子类化以创建我的自定义标记视图,但是有时它不起作用.
Ok, so here the problem is, I shouldn't have a red marker. Only green+blue markers.
I subclassed DefaultClusterRenderer
to create my custom marker view but sometimes it just doesn't work.
我正在使用毕加索来获取绿色图标,因为它来自API.但是问题是,当毕加索加载位图时,为时已晚,该图标已经设置为默认图标(红色).
I'm using picasso to get the green icon because it's coming from an API. But the problem is, when picasso has loaded the bitmap it's too late, the icon has already been set to the default one (red).
这是我的onBeforeClusterItemRenderer:
Here's my onBeforeClusterItemRenderer :
Picasso.with(getApplicationContext()).load(item.url).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
FrameLayout icon = (FrameLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.marker, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
icon.findViewById(R.id.bg).setBackground(new BitmapDrawable(getResources(), bitmap));
} else {
icon.findViewById(R.id.bg).setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
}
Bitmap b = createDrawableFromView(Home.this, icon);
if (marker != null) {
marker.icon(BitmapDescriptorFactory.fromBitmap(b));
}
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
推荐答案
---编辑---
在onBeforeClusterItemRendered
中下载图像时,实际上是每次Cluster Manager尝试加载标记时都下载图像,因此,例如,如果有100个标记,则将下载图像100次.
When downloading the image inside onBeforeClusterItemRendered
you are actually downloading the image every time the Cluster Manager tries to load a marker, so if you have, for example, 100 markers you will download the image 100 times.
您应该在onCreate
中下载图像,将其保存在静态变量中,保存图像后调用mClusterManager.cluster();
,最后在onBeforeClusterItemRendered
wrtie marker.icon(BitmapDescriptorFactory.fromBitmap(YourActivity.b));
You should download the image inside onCreate
, save it in a static variable, call mClusterManager.cluster();
after saving the image, and finally inside onBeforeClusterItemRendered
wrtie marker.icon(BitmapDescriptorFactory.fromBitmap(YourActivity.b));
这篇关于带有毕加索的Google Maps Cluster项目标记图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!