I need to use a bitmap image as Marker.icon .
i have 2 AsyncTask class , MyTask and BitmapTask , the first one retrieve a JSONArray with value { double,double,string,string = url of my bitmap icon } and the second one use the last string of JSONArray (in other words the url) as parameter the get the bitmap icon .
Trying to get it this way failed :
MyTask class
public class MyTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
// updateDisplay("Starting task");
//tasks.add(this);
}
@Override
protected String doInBackground(String... params) {
String content = HttpManager.getData(params[0]);
return content;
}
@Override
protected void onPostExecute(String result) {
double JsonLat = 0.0 ;
double Jsonlong = 0.0 ;
String JsonName = "" ;
String JsonIconurl = "" ;
Bitmap JsonIcon = null;
try {
JSONArray cast = new JSONArray(result);
BitmapTask icontask = new BitmapTask();
for (int i=0; i<cast.length(); i++) {
JSONObject Marker = new JSONObject(cast.get(i).toString());
stringJsonLat = Marker.getString("latitude");
stringJsonLng = Marker.getString("longitude");
Jsonlong = Double.parseDouble(stringJsonLng);
JsonLat = Double.parseDouble(stringJsonLat);
JsonName = Marker.get("title").toString();
JsonIconurl = Marker.get("icone").toString();
icontask.execute(JsonIconurl);
JsonIcon = icontask.doInBackground();
if (InArea(JsonLat,Jsonlong)) {
mMap.addMarker(new MarkerOptions().position(new LatLng(JsonLat,Jsonlong)).title(JsonName).icon(BitmapDescriptorFactory.fromBitmap(JsonIcon)));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected void onProgressUpdate(String... values) {
// updateDisplay(values[0]);
}
}
BitmapTask class
public class BitmapTask extends AsyncTask<String, Void, Bitmap>{
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bmImg = null;
try {
URL url = new URL(params[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
}
catch (IOException e)
{
e.printStackTrace();
bmImg = null;
}
return bmImg;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
// TODO: do what you need with resulting bitmap - add marker to map
}
};