我使用一个后台线程读取,我读我的database.The数据的一些数据是GPS(纬度,lobgitude)......每次读了一些新的数据我尝试显示使用RouteDraw()在该点绘制地图,并在我读取的点之间绘制一条线。在地图上绘制一条线
下面的代码是工作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
公共类screen_database延伸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();
}
}
你是对的...当我想多少时间,我已经花了这一点;) – adrian 2011-05-15 16:34:25