2

我开发了一个GPS应用程序,我在其中记录用户根并在地图上显示.......但是在查看我的路线时在地图上四处移动非常缓慢,至少需要 4 或 5 秒地图响应手指滑动......

我已经覆盖了该onDraw() 方法并绘制线条以显示路线......有没有更好的方法来做到这一点,以便平移变得更快"MyTracks"............

谢谢大家...... Pratap S.

4

4 回答 4

2

Comment to dalelane's answer from May,7th: I used your solution for reducing the load of drawing, but modified it a bit:

  • a new bitmap is created, if the map center, the zoom levelhave cjanged or no old bitmap exists.

After zooming the route is placed on the correct position. It seems that zooming has not finished completely, when a changed zoom level is detected.

I used a timer, which modifies the map center by 10 after a delay of 600 msecs after the zoom level changed. By changing the map center the draw method is called and creates a new bitmap. The route then is placed correctly. This is an ugly work around. Has anyone a better solution?

private void panAfterZoom(MapView mv, long delay){
    timer = new java.util.Timer("drawtimer", true);
    mapView=mv;
    task = new java.util.TimerTask() {
       public void run() {
           GeoPoint center=mapView.getMapCenter();
           GeoPoint point=new GeoPoint(center.getLatitudeE6()+10, center.getLongitudeE6());
           MapController contr=mapView.getController();
           contr.setCenter(point);
           timer.cancel();
       }
       };
    timer.schedule(task, delay);
}

This is called in the draw method as: pabAfterZoom(mapView, 600);

Bost

于 2010-08-13T10:29:46.263 回答
2

我不得不做类似的事情。我的尝试目前在 onDraw 中执行以下操作(为便于阅读而简化 - 错误处理等已被删除):

if ((bmap == null) || (lastZoom != mapv.getLatitudeSpan()))
{
    // bitmap is null - so we haven't previously drawn the path, OR
    //  the map has been zoomed in/out, so we're gonna re-draw it anyway
    //    (alternatively, I could have tried scaling the bitmap... might
    //     be worth investigating if that is more efficient)

    Projection proj = mapv.getProjection();

    // store zoom level for comparing in the next onDraw
    lastZoom = mapv.getLatitudeSpan();

    // draw a path of all of the points in my route
    GeoPoint start = routePoints.get(0);
    Point startPt = new Point();            
    proj.toPixels(start, startPt);

    Path path = new Path();
    path.moveTo(startPt.x, startPt.y);

    Point nxtPt;

    for (GeoPoint nextPoint : routePoints) 
    {
        nxtPt = new Point();
        proj.toPixels(nextPoint, nxtPt);
        path.lineTo(nxtPt.x, nxtPt.y);
    }

    // create a new bitmap, the size of the map view
    bmap = Bitmap.createBitmap(mapv.getWidth(), mapv.getHeight(), Bitmap.Config.ARGB_8888);

    // create an off-screen canvas to prepare new bitmap, and draw path on to it
    Canvas offscreencanvas = new Canvas(bmap);
    offscreencanvas.drawPath(path, mPaint);

    // draw the bitmap of the path onto my map view's canvas
    canvas.drawBitmap(bmap, 0, 0, null);

    // make a note of where we put the bitmap, so we know how much we 
    //  we need to move it by if the user pans the map
    mapStartPosition = proj.fromPixels(0, 0);
}
else
{
    // as we're in onDraw, we think the user has panned/moved the map
    //  if we're in here, the zoom level hasn't changed, and
    //   we've already got a bitmap with a drawing of the route path

    Projection proj = mapv.getProjection();

    // where has the mapview been panned to?
    Point offsetPt = new Point();
    proj.toPixels(mapStartPosition, offsetPt);

    // draw the bitmap in the new correct location
    canvas.drawBitmap(bmap, offsetPt.x, offsetPt.y, null);
}

它还不完美....例如,路径在缩放后立即出现在错误的位置 - 一旦用户开始平移,就会移动到正确的位置。

但这是一个开始——而且比在每次 onDraw 调用中重绘路径要高效得多

希望这可以帮助!

于 2010-05-07T10:54:22.290 回答
1

感谢 dalelane,他的上述建议帮助我改进了路线覆盖。我想分享一个改进,解决缩放更改后路径在错误位置结束的问题。

问题根本原因: mapview.getLatitudeSpan() 以及 mapview.getZoomLevel() 方法返回的值未考虑缩放值之间的渐进地图比例变化(动画)。

解决方案: 方法 mapview.getProjection().fromPixels(x,y) 考虑了这种渐进变化,因此您可以从中构建您的 getLatitudeSpan() 或 getLongitudeSpan(),并且路线将始终正确显示。

以下是 dalelane 建议的代码,其中进行了更改:

**int lonSpanNew = mapv.getProjection().fromPixels(0,mapv.getHeight()/2).getLongitudeE6() - mapv.getProjection().fromPixels(mapv.getWidth(),mapview.getHeight()/2).getLongitudeE6();**
if ((bmap == null) || (lastZoom != **lonSpanNew** )) 
{ 
    // bitmap is null - so we haven't previously drawn the path, OR 
    //  the map has been zoomed in/out, so we're gonna re-draw it anyway 
    //    (alternatively, I could have tried scaling the bitmap... might 
    //     be worth investigating if that is more efficient) 

    Projection proj = mapv.getProjection(); 

    // store zoom level for comparing in the next onDraw 
    lastZoom = **lonSpanNew**;  

    // draw a path of all of the points in my route 
    GeoPoint start = routePoints.get(0); 
    Point startPt = new Point();             
    proj.toPixels(start, startPt); 

    Path path = new Path(); 
    path.moveTo(startPt.x, startPt.y); 

    Point nxtPt; 

    for (GeoPoint nextPoint : routePoints)  
    { 
        nxtPt = new Point(); 
        proj.toPixels(nextPoint, nxtPt); 
        path.lineTo(nxtPt.x, nxtPt.y); 
    } 

    // create a new bitmap, the size of the map view 
    bmap = Bitmap.createBitmap(mapv.getWidth(), mapv.getHeight(), Bitmap.Config.ARGB_8888); 

    // create an off-screen canvas to prepare new bitmap, and draw path on to it 
    Canvas offscreencanvas = new Canvas(bmap); 
    offscreencanvas.drawPath(path, mPaint); 

    // draw the bitmap of the path onto my map view's canvas 
    canvas.drawBitmap(bmap, 0, 0, null); 

    // make a note of where we put the bitmap, so we know how much we  
    //  we need to move it by if the user pans the map 
    mapStartPosition = proj.fromPixels(0, 0); 
} 
else 
{ 
    // as we're in onDraw, we think the user has panned/moved the map 
    //  if we're in here, the zoom level hasn't changed, and 
    //   we've already got a bitmap with a drawing of the route path 

    Projection proj = mapv.getProjection(); 

    // where has the mapview been panned to? 
    Point offsetPt = new Point(); 
    proj.toPixels(mapStartPosition, offsetPt); 

    // draw the bitmap in the new correct location 
    canvas.drawBitmap(bmap, offsetPt.x, offsetPt.y, null); 
}

希望这有帮助。问候,路易斯

于 2012-07-02T01:09:31.953 回答
0

覆盖 onDraw 将是唯一的方法。你是如何绘制轨道的,也许可以提高效率?

于 2010-01-04T14:26:03.713 回答