I'm trying to show images (downloaded from a server) as map markers on a Google Map. I'm using Android-Maps-Extensions because i wasn't happy with Google Map-Utils performance.
Now i'm wondering how to load map marker images aynchronously with 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
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
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.
On a sidenode: I'm using Volley to dl the images asynchronously.
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
We will load icons with Glide, I found it more stable than Picasso.
Call loadIconMarker for every marker you are adding to the map.
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. This is my ClusterOptionsProvider.
I use base bitmap R.drawable.m1 and add text on it representing number of markers in cluster.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With