【问题标题】:disperately trying to draw a line on the map拼命想在地图上画一条线
【发布时间】:2011-05-10 10:50:03
【问题描述】:

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

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

这是我的代码的一部分:

地理点 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);


    }

}

我的问题:

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

【问题讨论】:

    标签: android map paint


    【解决方案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);
    

    你可以从你的活动类中添加地理点

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

    【讨论】:

    • @adrian :如果您没有收到任何错误消息,请检查您的覆盖类及其绘制函数,检查它们是否正确调用。尝试使用断点并检查 logcats
    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 2011-09-30
    相关资源
    最近更新 更多