problem with AsyncTask
I have the following code in which I read some data from a database(longitude,latitude) and try to draw a line between those points in the UI thread:
The functions that draw that line are :
public void theRouteDraw(List<GeoPoint> geoPointsArray)
{
for (int i = 0; i < geoPointsArray.size(); i++)
{
p = geoPointsArray.get(i);
mc.animateTo(p);
mc.setZoom(13);
mapView.invalidate();
mapView.setSatellite(true);
}
}
and
private void initMyLocation()
{
myLocOverlay = new MyLocationOverlay(this, mapView);
myLocOverlay.enableMyLocation();
mapView.getOverlays().add(new myLocOverlay());
}
Beside those 2 methods here is how my code looks like:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_database);
db = new DBAdapter(this);
progress = new ProgressDialog(this);
progress.setIndeterminate(true);
progress.setMessage("I am thinking");
InitTask init_task = new InitTask();
init_task.execute(db);
initMap();
}
private void initMap()
{
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mc = mapView.getController();
}
my Async classs
public class InitTask extends AsyncTask<DBAdapter, GeoPoint, List<GeoPoint>>
{
List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
DBAdapter db;
int latitude;
int longitude;
GeoPoint p;
protected void onPreExecute()
{
progress.show();
}
protected List<GeoPoint> doInBackground(DBAdapter... db)
{
try
{
db[0].openDataBase();
Cursor c = db[0].getAllData();
if (c.moveToFirst())
{
do
{
longitude = Integer.parseInt(c.getString(0));
latitude = Integer.parseInt(c.getString(1));
p = new GeoPoint(latitude, longitude);
geoPointsArray.add(p);
}
while (c.moveToNext());
}
c.close();
db[0].close();
}
catch (Exception e)
{
Log.d("Eroare", "doInBackground", e);
}
return geoPointsArray;
}
protected void onPostExecute(List<GeoPoint> geoPointsArray)
{
// super.onPostExecute(geoPointsArray);
progress.dismiss();
Log.d("Lista", new Boolean(geoPointsArray.isEmpty()).toString());
theRouteDraw(geoPointsArray);
initMyLocation();
}
}
Questions:
1.Why don't i have my route drawn on the map.....cause I checked and in onPostExecute() I have all the points from DB
2.How could I retrieve this points step by step ....I mean retrieve some points update my UI and retrieve other points and update my UI...?
Thank u!!
UPDATE:
protected List<GeoPoint> doInBackground(DBAdapter... db)
{
try
{
db[0].openDataBase();
Cursor c = db[0].getAllData();
if (c.moveToFirst())
{
do
{
longitude = Integer.parseInt(c.getString(0));
latitude = Integer.parseInt(c.getString(1));
p = new GeoPoint(latitude, longitude);
geoPointsArray.add(p);
publishProgress(p);
SystemClock.sleep(1000);
}
while (c.moveToNext());
}
c.close();
db[0].close();
}
catch (Exception e)
{
Log.d("Eroare", "doInBackground", e);
}
return geoPointsArray;
}
I use publishProgress(p); to send the coordinates to :
protected void onProgressUpdate(GeoPoint... progress)
{
theRouteDraw(progress1[0]);
int lon=progress1[0].getLongitudeE6();
int lat=progress1[0].getLatitudeE6();
GeoPoint p2=new GeoPoint(lon,lat);
geoPointsArray.add(p2);
initMyLocation();
}
Now,I displayed and I get them step by step(which is what I wanted) now if I could just figure out why I don't get the route displayed I would be a happy man.And I haven't been happy for a long time.Thx:D
UPDATE2:
class myLocOverlay extends Overlay{
public myLocOverlay(){
}
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
Projection projection = mapView.getProjection();
Path p1 = new Path();
for (int i = 0; i < geoPointsArray.size(); i++) {
if (i == geoPointsArray.size() - 1) {
break;
}
Point from = new Point();
Point to = new Point();
projection.toPixels(geoPointsArray.get(i), from);
projection.toPixels(geoPointsArray.get(i + 1), to);
开发者_如何转开发 p1.moveTo(from.x, from.y);
p1.lineTo(to.x, to.y);
}
Paint mPaint = new Paint();
mPaint.setStyle(Style.STROKE);
mPaint.setColor(Color.GREEN);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(4);
canvas.drawPath(p1, mPaint);
super.draw(canvas, mapView, shadow);
}
}
in method initMyLocation() I add an overlay to the current position.....the overlay is obtained from the class myLocOverlay()...
myLocOverlay() runs through the geoArrayPoints and draws a line between them!!!
The method RouteDraw() puts on the map the new geopoints that I get....and it works just fine!
精彩评论