1

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!

adrian
  • 4,574
  • 17
  • 68
  • 119
  • 1
    Are you aware, that the `doInBackground` method is executed on a background thread, while the `onPre-` and `onPostExecute` methods run on the UI thread? You could use the `doInBackground` to retrieve the data from the database, and the `onPostExecute` method to draw the line on the map based on the retrieved points. – rekaszeru May 08 '11 at 11:26
  • I'm aware...and that is exactly what I'm doing:) – adrian May 08 '11 at 11:29
  • sorry, at first glance I didn't find your `onPostExecute` method because of the formatting, and that's why I was thinking all the code is inside `doInBackground`. – rekaszeru May 08 '11 at 11:41
  • The two function that do the drawing worked when I used them directly in onCreate() and the data was read within the UI thread....now I get nothing displayed...thx – adrian May 08 '11 at 11:45
  • When debugging, in `doInBackground`, `onPostExecute` and respectively in `theRouteDraw` methods you get the list of `GeoPoint` instances correctly? – rekaszeru May 08 '11 at 12:05
  • yes I do:)....I displayed them in the logcat...I changed a little bit my code....I'll edit my question:) – adrian May 08 '11 at 12:09
  • for your update to work, you must have set the second generic type argument of your `AsyncTask` implementation from `Void` to `GeoPoint`. You should update that in your question too. – rekaszeru May 08 '11 at 13:25
  • also, you should take a look at the following solutions [by @Mathias Lin](http://stackoverflow.com/questions/3109158/how-to-draw-a-path-on-a-map-using-kml-file/3109723#3109723) or [from synyx.de](http://mobile.synyx.de/2010/06/routing-driving-directions-on-android-%E2%80%93-part-2-draw-the-route/) – rekaszeru May 08 '11 at 13:29
  • I saw that u are from Romania....we can talk romanian,putem?;)))Mersi:) – adrian May 08 '11 at 14:34
  • In case of a solution you should publish it in English, though, so everybody who's interested could learn from it. Deci sigur, daca crezi ca va fi mai usor cu terminologia (eu habar n-am de ceea romana), putem sa incercam. :) – rekaszeru May 08 '11 at 14:57
  • Ar fi mai bine sa scrii mesaje pe aces thread, asa primim amandoi notificare :). Si te rog sa-mi trimiti linkul mediafire.com de unde pot descarca proiectul, ca sa vad unde ar putea fi problema. multumesc! (te rog sa stergi comentul de pe threadul celalalt, mersi) – rekaszeru May 10 '11 at 12:09
  • http://www.mediafire.com/?09ofnrt9o92smct Aici gasesti proiectul horror.Ms:) – adrian May 10 '11 at 12:17
  • Salut....ai reusit sa faci ceva cu proiectul ala?Ms – adrian May 12 '11 at 13:19
  • use a handler to communicate to the main UI thread i.e send the co-ordinates the main thread and they will be ploted. – Fred Ondieki Feb 06 '14 at 20:13

0 Answers0