Android地图扩展异步加载标记图像
我试图在谷歌地图上显示图像(从服务器下载)作为地图标记。我使用安卓地图扩展是因为我对谷歌地图Utils的性能不满意 现在,我想知道如何使用AME同步加载地图标记图像。任务是显示图像本身(如果它不是集群)。 如果它是一个簇,我想显示第一个标记的图像和簇中元素的数量 到目前为止,我已经做了以下工作: 1) 设置地图簇Android地图扩展异步加载标记图像,android,google-maps,google-maps-markers,android-maps-v2,android-maps-extensions,Android,Google Maps,Google Maps Markers,Android Maps V2,Android Maps Extensions,我试图在谷歌地图上显示图像(从服务器下载)作为地图标记。我使用安卓地图扩展是因为我对谷歌地图Utils的性能不满意 现在,我想知道如何使用AME同步加载地图标记图像。任务是显示图像本身(如果它不是集群)。 如果它是一个簇,我想显示第一个标记的图像和簇中元素的数量 到目前为止,我已经做了以下工作: 1) 设置地图簇 private void setupMapClusterer() { mClusterOptions = new EClusterOptionsProvider(getCont
private void setupMapClusterer() {
mClusterOptions = new EClusterOptionsProvider(getContext());
ClusteringSettings clusteringSettings = new ClusteringSettings();
clusteringSettings.addMarkersDynamically(true);
clusteringSettings.clusterOptionsProvider(mClusterOptions);
mMap.setClustering(clusteringSettings);
}
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;
}
public EClusterOptionsProvider(上下文){
mClusterOptions=新的ClusterOptions();
mContext=上下文;
mIconGenerator=新的IconGenerator(上下文);
//充气布局
View-markerView=LayoutFlater.from(上下文)。充气(R.layout.map\u标记,空);
mImageView=(ImageView)markerView.findviewbyd(R.id.map\u marker\u image);
mTextView=(TextView)markerView.findviewbyd(R.id.map\u marker\u text);
//设置图标生成器
setContentView(markerView);
}
公共位图创建图标(位图bmp,字符串文本){
设置图像位图(bmp);
mTextView.setText(文本);
返回mIconGenerator.makeIcon();
}
@凌驾
公共ClusterOptions getClusterOptions(列表){
//从第一个标记获取位图
Marker first=list.get(0);
mClusterOptions.icon(BitmapDescriptorFactory.fromBitmap(mIconGenerator.makeIcon());
返回mClusterOptions;
}
这给了我一个地图,其中集群有我的自定义视图(这是正确的),但我不知道在哪里下载图像,以及如何将它们放入单个标记,尤其是集群
当然,我不想提前下载所有图片(我们谈论的是500多张图片)
在一个侧节点上:我正在使用Volley异步地dl图像。我创建了一个使用集群和异步加载标记的简单示例。
希望你在这里找到一些有用的 我们将使用Glide加载图标,我发现它比毕加索更稳定。
为要添加到地图的每个标记调用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);
}
});
}
谢谢,但我认为它仍在加载地图上每个标记的图像。如果有200个图像的集群,此代码将为每个图像加载位图,其中一个图像(第一个)就足够了。@AndreasKühntopf当然,图像将只下载一次,然后glide将从缓存中获取它。但没有人阻止您创建自己的缓存:)
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);
}
}