谷歌地图v2自定义瓦片提供程序

4
我正在开发一个自定义瓦片提供程序,以在Google地图上显示交通数据。在高缩放级别下,效果很好。good one 但是,在低缩放级别下,折线会重叠。overlappig 我的自定义瓦片提供程序类为:
public class PolylineTileProvider implements TileProvider {
private static final String TAG = "TileOverlay";
private final int mTileSize = 256;
private final SphericalMercatorProjection mProjection = new SphericalMercatorProjection(mTileSize);
private final int mScale = 2;
private final int mDimension = mScale * mTileSize;
private final List<PolylineOptions> polylines;

public PolylineTileProvider(List<PolylineOptions> polylines) {
    this.polylines = polylines;
}

@Override
public Tile getTile(int x, int y, int zoom) {
    Matrix matrix = new Matrix();
    float scale = ((float) Math.pow(2, zoom) * mScale);
    matrix.postScale(scale, scale);
    matrix.postTranslate(-x * mDimension, -y * mDimension);
    Bitmap bitmap = Bitmap.createBitmap(mDimension, mDimension, Bitmap.Config.ARGB_8888); //save memory on old phones
    Canvas c = new Canvas(bitmap);
    c.setMatrix(matrix);
    drawCanvasFromArray(c, scale);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return new Tile(mDimension, mDimension, baos.toByteArray());
}

private void drawCanvasFromArray(Canvas c, float scale) {

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setShadowLayer(0, 0, 0, 0);
    paint.setAntiAlias(true);

    if (polylines != null) {
        for (int i = 0; i < polylines.size(); i++) {
            List<LatLng> route = polylines.get(i).getPoints();
            paint.setColor(polylines.get(i).getColor());
            paint.setStrokeWidth(getLineWidth(polylines.get(i).getWidth(), scale));
            Path path = new Path();
            if (route != null && route.size() > 1) {
                Point screenPt1 = mProjection.toPoint(route.get(0)); //first point
                MarkerOptions m = new MarkerOptions();
                m.position(route.get(0));
                path.moveTo((float) screenPt1.x, (float) screenPt1.y);
                for (int j = 1; j < route.size(); j++) {
                    Point screenPt2 = mProjection.toPoint(route.get(j));
                    path.lineTo((float) screenPt2.x, (float) screenPt2.y);
                }
            }
            c.drawPath(path, paint);
        }
    }
}

private float getLineWidth(float width, float scale) {
    return width / (scale);
}
}

谷歌地图安卓应用程序的交通图层非常好。

我该如何制作类似的图层?提前感谢。

1个回答

0

它变得模糊或者在屏幕上不再可见的原因是因为你创建了一个图像,然后使用提供的矩阵进行缩放。

相反,你不应该使用矩阵,而是生成正确尺寸的图像。

为此,从Canvas中删除setMatrix调用,并使用正确缩放的坐标将点添加到路径中。

 x = screenPt1.x * scale - x * mDimension;
 y = screenPt1.y * scale - y * mDimension;

那么您将在每个缩放级别上获得指定的确切行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接