拼命想在地图上划一条线

时间:2011-05-10 10:50:03

标签: android map paint

我正在从存储它的ServerManager类中读取一些GPS数据(经度,纬度)。

读取是在后台线程AsyncTask中完成的,并且是逐步完成的。每次我得到一个新点我把它放在地图上并且我在它和最后一个点之间画一条线。

以下是我的代码的一部分:

GeoPoint p;

protected Void doInBackground(Void... voids) {

    try {

        while (true) {
            longitude = Integer.parseInt(ServerManager.getInstance()
                    .getLastLongitude());
            latitude = Integer.parseInt(ServerManager.getInstance()
                    .getLastLatitude());
            Log.d("Date citite de threadul AsyncTask", " ");
            System.out.println(longitude);
            System.out.println(latitude);
            p = new GeoPoint(longitude, latitude);
            publishProgress(p);
            Thread.sleep(500);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

protected void onProgressUpdate(GeoPoint... progress1) {

    theRouteDraw(progress1[0]);
  geoPointsArray.add(progress1[0]);

  if (geoPointsArray.size() > 2) {
            int length = geoPointsArray.size();

            mapView.getOverlays().add(
                    new myOverlay(geoPointsArray.get(length - 1),
                            progress1[0]));
        }

}

}

为了表示我使用的地图上的点:

public void theRouteDraw(GeoPoint p1){

mc.animateTo(p1);

mc.setZoom(17);

mapView.invalidate();

mapView.setSatellite(true);

}


为了获得叠加,我使用:

class myOverlay扩展了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) {
        super.draw(canvas, mapView, shadow);

        Paint mPaint = new Paint();
        mPaint.setStyle(Style.STROKE);
        mPaint.setColor(Color.GREEN);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(4);
        Projection projection = mapView.getProjection();

        Point from = new Point();

        projection.toPixels(gp1, from);

        Point to = new Point();

        projection.toPixels(gp2, to);

        canvas.drawLine(from.x, from.y, to.x, to.y, mPaint);


    }

}

我的问题:

有没有人知道为什么我的地图上没有画线?谢谢你:)

1 个答案:

答案 0 :(得分:1)

试试这个:

class myLocOverlay extends Overlay { public myLocOverlay(GeoPoint gp1,GeoPoint gp2) {
   this.gp1 = gp1;
this.gp2 = gp2;
}

并使用此

更新onDraw功能
Paint paint = new Paint(); Point point = new Point();
        projection.toPixels(gp1, point); Point point2 = new Point();
            projection.toPixels(gp2, point2); canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);

您可以从活动类

添加geopoint
mMapView01.getOverlays().add(new MyOverLay(startGP,endGP));