在地图上画一条线

发布于 2024-11-06 02:50:15 字数 4211 浏览 0 评论 0原文

我正在使用后台线程从数据库中读取一些数据。我读取的数据是 GPS(纬度,经度)...每次读取一些新数据时,我都会尝试使用 theRouteDraw() 显示该点的地图,还可以在我读到的点之间画一条线。

以下代码有效!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

公共类 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();
}

}

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();
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灵芸 2024-11-13 02:50:15

您在地图上看不到线条的原因是,当您使用两个 GeoPoint 创建新的 myOverlay 时,您实际上使用的是相同的两个点。在这一行中:

mapView.getOverlays().add(new myOverlay(geoPointsArray.get(1),geoPointsArray.get(1)));

很明显,相同的点被添加为起点和终点 - 我想当数组中只有 1 个点(这是您的起点)时,这是可以的。但是,代码:

overlays.add(new myOverlay(geoPointsArray.get(i - 1), progress1[0]));

也使用相同的点,因为您刚刚将新点 Progress1[0] 添加到了之前的这一行中的 geoPointsArray 中:

geoPointsArray.add(progress1[0]);

因此 canvas.drawLine 不会产生可见的结果。

如果将上面的行移动到方法的末尾,那么您将不会在 myOverlay 构造函数中对 gp1 和 gp2 使用相同的点。您必须稍微整理一下顺序。我使用了一组预先构建的 GeoPoints 数组,而不是从数据库中获取它们,我发现 AsyncTask 有点不可预测 - 这是我的尝试的屏幕截图(地图没有显示,因为我没有使用正确的方法)地图 API 密钥:

在此处输入图像描述

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:

mapView.getOverlays().add(new myOverlay(geoPointsArray.get(1),geoPointsArray.get(1)));

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:

overlays.add(new myOverlay(geoPointsArray.get(i - 1), progress1[0]));

is also using the same point, since you just added the new point progress1[0] to the geoPointsArray in this line which comes before:

geoPointsArray.add(progress1[0]);

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:

enter image description here

幸福丶如此 2024-11-13 02:50:15

首先你的问题不太清楚。
但是,您可能想查看 myLocOverlay 中的 for 循环(& u 有一个同名变量!不酷),您的 geoPointsArray.size() 可能不会超过 1,因此循环始终休息。我还可能建议您首先使用 AsyncTask 获取所有 GeoPoints,然后再继续进行任何类型的叠加(特别是如果您的数据大小不是太大并且不需要太多时间)。

First your question isn't quite clear.
However you might want to look into the for loop you have in the myLocOverlay ( & u have a variable of the same name! not cool) it is possible your geoPointsArray.size() doesn't get beyond 1, hence the loop always break. I might also suggest you use the AsyncTask to get all your GeoPoints first, before proceeding with doing any sort of overlays (esp if your data size is not too much and doesn't take much time).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文