2017-01-10 71 views
3

我正在開發自定義瓷磚供應商以顯示Google地圖上的流量數據。在高縮放級別對我來說很好。 good one但折線在低級縮放時重疊。 overlappig谷歌地圖v2自定義瓷磚供應商

我定製的瓷磚供應商類是

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層是在谷歌地圖Android應用那麼好所示。

我該如何製作一個類似的圖層。提前致謝。

回答

0

爲什麼它變得模糊,或者可能沒有在屏幕上看到更多的原因是因爲您創建了一個圖像,然後使用您提供的矩陣進行縮放。

相反,您不應該使用矩陣並生成正確大小的圖像。

待辦事項如此,請刪除您在畫布上的setMatrix調用 和 將點添加到具有正確縮放座標的路徑中。

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

然後,您會在每個縮放級別獲得指定的確切線。

相關問題