My app requires adding of hundreds markers (in some cases). For this I am using "for loop" which seems to be not bad solution if I am using default markers. I need to use custom image for each marker which I am downloading from the internet with the helping of AsynkTask
class Loader()
. But when I am using custom markers the UI freezes until "for loop" will be finished.
Adding markers:
private List<User> mUsers = new ArrayList<User>();
private GoogleMap mMap;
Bitmap bAvatar;
.............
private void drawMarkers() {
mMap.clear();
for (User user : mUsers) {
if (user.getGeohash() != null) {
LatLng pos = gh.decode(user.getGeohash());
if (user.getAvatar() != null) {
try {
bAvatar = new Loader().execute(user.getAvatar()).get();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ExecutionException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mMap.addMarker(new MarkerOptions()
.position(pos)
.title(user.getStatus())
.icon(BitmapDescriptorFactory.fromBitmap(bAvatar)));
} else {
mMap.addMarker(new MarkerOptions()
.position(pos)
.title(user.getStatus())
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
}
}
}
AsyncTask for image downloading:
public class Loader extends AsyncTask<String, Void, Bitmap> {
Bitmap bitmap;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
mIcon11 = BitmapFactory.decodeStream(in, null, options);
} catch (Exception e) {
String error = (e.getMessage() == null) ? "doInBackground - Loader"
: e.getMessage();
Log.e("Error", error);
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bitmap = result;
}
}
I assume that I need to replace "for loop" with the AsynkTask
where each user from List<User>
will be downloaded asynchronously.
- Please help me to create
AsynkTask
forList<User>
array. - Should I combine
AsynkTask
forList<User>
withAsynkTask
for image downloading or is it correct to useAsynkTask
insideAsynkTask
?
Any help will be appreciated. Thanks in advance.