Google Maps v2自定义平铺提供程序

时间:2017-01-10 13:44:19

标签: android google-maps overlay google-polyline

我正在开发一个自定义磁贴提供程序,以在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);
}
}

Trafic图层在Google Maps Android应用程序中显示得非常好。

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

1 个答案:

答案 0 :(得分:0)

它被模糊的原因,或者可能在屏幕上看不到的原因是因为您创建了一个图像,然后使用您提供的矩阵进行缩放。

相反,你不应该使用矩阵并生成正确大小的图像。

Todo所以,删除你在Canvas上的setMatrix调用 和 使用正确的缩放坐标将点添加到路径。

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

然后在每个缩放级别获得指定的确切行。