AsyncTask 的问题
我有以下代码,其中我从数据库读取一些数据(经度、纬度),并尝试在 UI 线程中的这些点之间画一条线:
绘制该线的函数是:
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);
}
}
方法
private void initMyLocation()
{
myLocOverlay = new MyLocationOverlay(this, mapView);
myLocOverlay.enableMyLocation();
mapView.getOverlays().add(new myLocOverlay());
}
除了这两个方法之外,这里是我的 代码如下:
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();
}
我的异步类
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();
}
}
问题:
1.为什么我没有在地图上绘制路线......因为我检查过并在 onPostExecute() 中我拥有来自 DB 的所有点
2.我怎样才能检索到这个一步一步点....我的意思是检索一些点更新我的 UI 并检索其他点并更新我的 UI...?
感谢你!!
更新:
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;
}
我使用publishProgress(p);将坐标发送到:
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();
}
现在,我显示并逐步获取它们(这就是我想要的)现在,如果我能弄清楚为什么我没有显示路线,我将是一个快乐的人。而且我还没有很久没有高兴了。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);
}
}
在方法 initMyLocation() 中我向当前位置添加了一个覆盖层......覆盖层是从类 myLocOverlay() 获得的...
myLocOverlay() 遍历 geoArrayPoints 并在它们之间画一条线!!!
RouteDraw() 方法将我得到的新地理点放在地图上......并且它工作得很好!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论