I’m using a background thread to read some data from my database.The data that I read is GPS(latitude,lobgitude)…each time a read some new data I try display the map in that point using theRouteDraw() and also to draw a line between the points that I read.
The following code is working!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public class screen_database extends MapActivity {
private LocationManager lm;
private LocationListener locationListener;
private MyLocationOverlay myLocOverlay;
private MapView mapView;
private MapController mc;
DBAdapter db;
InitTask init_task;
GeoPoint p;
GeoPoint progress1[];
List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
GeoPoint srcPlace;
GeoPoint destPlace;
double latitude;
double longitude;
private ProgressDialog progress;
MotionEvent event;
@Override
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");
initMap();
// initMyLocation();
// theRouteDraw();
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private void initMap() {
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mc = mapView.getController();
}
public void onResume() {
super.onResume();
init_task = new InitTask();
init_task.execute(db);
}
public void theRouteDraw(GeoPoint p) {
mc.animateTo(p);
mc.setZoom(17);
mapView.setSatellite(true);
mapView.setStreetView(true);
mapView.invalidate();
}
class myOverlay extends Overlay {
GeoPoint gp1;
GeoPoint gp2;
public myOverlay(GeoPoint gp1, GeoPoint gp2) {
this.gp1 = gp1;
this.gp2 = gp2;
}
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
Paint mPaint = new Paint();
Point from = new Point();
projection.toPixels(gp1, from);
mPaint.setColor(Color.BLUE);
Point to = new Point();
projection.toPixels(gp2, to);
mPaint.setStrokeWidth(9);
mPaint.setAlpha(120);
canvas.drawLine(from.x, from.y, to.x, to.y, mPaint);
super.draw(canvas, mapView, shadow);
}
}
public class InitTask extends AsyncTask<DBAdapter, GeoPoint, Void> {
List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
DBAdapter db;
int latitude;
int longitude;
GeoPoint p;
protected void onPreExecute() {
progress.show();
}
protected Void doInBackground(DBAdapter... db) {
try {
db[0].openDataBase();
Cursor c = db[0].getAllData();
if (c.moveToFirst()) {
do {
longitude =Integer.parseInt(c.getString(1));
latitude = Integer.parseInt(c.getString(2));
p = new GeoPoint(latitude, longitude);
geoPointsArray.add(p);
publishProgress(p);
Thread.sleep(1500);
} while (c.moveToNext());
}
c.close();
db[0].close();
} catch (Exception e) {
Log.d("Eroare", "doInBackground", e);
}
return null;
}
protected void onProgressUpdate(GeoPoint... progress1) {
try{
if(geoPointsArray.size()==1){
mapView.getOverlays().add(new myOverlay(geoPointsArray.get(1),geoPointsArray.get(1)));
theRouteDraw(progress1[0]);
}
}
catch(Exception e){
e.printstack();
}
if (geoPointsArray.size() > 1) {
int i = geoPointsArray.size();
List overlays = mapView.getOverlays();
overlays.add(new myOverlay(geoPointsArray.get(i - 1),
progress1[0]));
theRouteDraw(progress1[0]);
}
geoPointsArray.add(progress1[0]);
}
}
protected void onPause() {
init_task.cancel(true);
super.onPause();
}
}
the reason you don’t see a line on the map is because when you create the new myOverlay with the two GeoPoints, you’re actually using the same two points. In this line:
its obvious that the same point is being added as the from and to points – which I guess is okay when you only have 1 point in the array (this is your start point). However, the code:
is also using the same point, since you just added the new point progress1[0] to the geoPointsArray in this line which comes before:
So canvas.drawLine won’t produce a visible result.
If you move the line above to the end of the method then you won’t be using the same point for gp1 and gp2 in the myOverlay constructor. You will have to sort out the sequencing a bit. I used an array of pre-constructed GeoPoints instead of getting them from the database and I found that the AsyncTask was a bit unpredictable – here’s a screen-shot of my attempt (the map doesn’t show because I’m not using my correct map API key: