android – Google Maps v2 Custom Tile Provider
•
Android
I'm developing a custom tile provider to display traffic data on Google maps. At a high zoom level, it's good for me
My custom tile provider class is
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);
}
}
The trafic layer displays very well in the Google Maps Android application
How can I make a similar layer? Thank you in advance
resolvent:
The reason it is blurred, or may not be visible on the screen, is because you create an image and zoom it using the matrix you provide
Instead, you should not use matrices and generate images of the correct size
Todo, so delete your setmatrix call on canvas and add points to the path using the correct scaling coordinates
x = screenPt1.x * scale - x * mDimension;
y = screenPt1.y * scale - y * mDimension;
The specified exact row is then obtained at each zoom level
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码