Android MapView overlay disappearing when zooming in

做~自己de王妃 提交于 2019-12-02 14:53:17

问题


I'm working on a simple Android app for plotting routes on a map. All is going well, but I have an issue when zooming in on my Samsung Galaxy S2. It works fine on a Galaxy S3, so I'm wondering whether it's related to memory management on the lower specced device. It also works fine on the emulator.

Here is equivalent code located in the overlays onDraw method, just condensed for posting here:

Point current = new Point();
Path path = new Path();
Projection projection = mapView.getProjection();

Iterator<GeoPoint> iterator = pointList.iterator();
if (iterator.hasNext()) {
    projection.toPixels(iterator.next(), current);
    path.moveTo((float) current.x, (float) current.y);
} else return path;
while(iterator.hasNext()) {
    projection.toPixels(iterator.next(), current);
    path.lineTo((float) current.x, (float) current.y);
}

Paint roadPaint = new Paint();
roadPaint.setAntiAlias(true);
roadPaint.setStrokeWidth(8.0f);
roadPaint.setColor(Color.BLACK);
roadPaint.setStyle(Paint.Style.STROKE);

canvas.drawPath(path, roadPaint);

It's not too dissimilar to most of the sample code floating around for doing this. I'm just wondering if anyone can confirm my suspicions and advise if there is anything I can do in terms of configuration or tweaks that I can do to force drawing at all zoom levels?

Thanks in advance.

Cheers, Nathan


回答1:


You said that code above was an equivalent (not the real code you are running) and that's clear because you are returning a Path object in a onDraw() which you couldn't.

The "compressed form" of code you show should work as well as using the drawLine(). So the problem should come from something else (may the original code).

Anyway, I'll give you a couple of hints:

  • When the top and bottom of object you are drawing to a canvas are both out of screen, the object is ignored and not drawn. Check if this is not whats happening with your path. See my answer in this post Android Map Overlay Disappears on Zoom
  • You don't need to rebuild the path object every time. You are probably already doing it, and that's why you made the short version above. See my answer in this post with some suggestions to improve path drawing: Overlay behavior when zooming
  • If for some reason you really want to use the slower approach of drawLine(), you can use the follwing to make the line look better:

    paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setColor(...);
    paint.setAlpha(...);
    paint.setStrokeWidth(...);
    

Finally, if the issue remains, update your question with more relevant code and let me know. Maybe I can help further.

Regards.




回答2:


The problem is that you are painting the overlay yourself for a very specific state of the mapview. You should use OverlayItem instead.

The OverlayItem is added to the MapView overlays collection, and the MapView handles all the re-drawing depending on it's own state ( zoom, location, etc )

@Override
public void draw( Canvas canvas, MapView mapView, boolean shadow )
{
    super.draw( canvas, mapView, shadow );

    int x1 = -1;
    int y1 = -1;
    int x2 = -1;
    int y2 = -1;

    Paint paint = new Paint();
    paint.setStyle( Paint.Style.STROKE );
    paint.setColor( GeoLocation.ROUTE_COLOR );
    paint.setStrokeWidth( STROKE_WIDTH );

    for ( int i = 0; i < mRouteGeoPoints.size(); i++ )
    {
        Point point = new Point();
        mapView.getProjection().toPixels( geoPoints.get( i ), point );
        x2 = point.x;
        y2 = point.y;
        if ( i > 0 )
        {
            canvas.drawLine( x1, y1, x2, y2, paint );
        }
        x1 = x2;
        y1 = y2;
    }
}


来源:https://stackoverflow.com/questions/13513578/android-mapview-overlay-disappearing-when-zooming-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!