2016-03-28 41 views
1

我想對兩個標記周圍的GoogleMaps應用色調。到目前爲止,唯一的工作解決我是真正在地圖上繪製多邊形作爲GoogleMaps添加色調

map.addPolygon(new PolygonOptions() 
       .addAll(createRectangle(SphericalUtil.interpolate(markerData.getStartPoint(), markerData.getEndPoint(), CENTER_POINT_INTERPOLATE), 90, 45)) 
       .strokeColor(Color.TRANSPARENT) 
       .fillColor(resources.getColor(R.color.tint_black_40))); 

其中createRectangle()https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/PolygonDemoActivity.java

private List<LatLng> createRectangle(LatLng center, double halfWidth, double halfHeight) { 
    return Arrays.asList(new LatLng(center.latitude - halfHeight, center.longitude - halfWidth), 
     new LatLng(center.latitude - halfHeight, center.longitude + halfWidth), 
     new LatLng(center.latitude + halfHeight, center.longitude + halfWidth), 
     new LatLng(center.latitude + halfHeight, center.longitude - halfWidth), 
     new LatLng(center.latitude - halfHeight, center.longitude - halfWidth)); 
} 

但是創建覆蓋方法沒有覆蓋整個地圖預覽 map preview。有沒有人可以幫助我改進現有方法或提出更好的解決方法?

+0

那麼,您是否需要用半透明顏色覆蓋完整的地圖? – antonio

回答

1

您可以創建TileOverlay並將其添加到地圖(根據zIndex,在您的標記之上或之下)。

這個想法是創建一個TileProvider,它總是返回一個非常小的(在我的例子中是2x2像素)圖像,將在地圖上繪製。爲確保性能,由TileProvider返回的Tile將始終相同。

下面是代碼:

public class BackgroundTileProvider implements TileProvider { 
    private Tile tile; 

    @Override 
    public Tile getTile(int x, int y, int zoom) { 
     if (tile == null) { 
      // Create a very small (for performance) bitmap with alpha 
      Bitmap bmp = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888); 

      // Draw the desired color to use as background 
      Canvas canvas = new Canvas(bmp); 
      canvas.drawColor(Color.argb(150, 0, 0, 0)); 

      // Get the bytes and create the Tile 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 

      tile = new Tile(2, 2, stream.toByteArray()); 
     } 

     return tile; 
    } 
} 

您需要將TileOverlay添加到您的地圖如下:

TileProvider tileProvider = new BackgroundTileProvider(); 
TileOverlay tileOverlay = map.addTileOverlay(
    new TileOverlayOptions().tileProvider(tileProvider)); 
+0

謝謝@antonio這確實有效。根據我之前附上的截圖,右側的地圖現在已經着色。但是左側有條紋,沒有着色。這似乎與'animateCamera'之後的地圖渲染有關。具有色調的地圖的初始渲染是很好的,只有居中和縮放應用左側鬆動着色瓷磚。儘管如此,上面幫了我很多。 Thanx –

+0

很奇怪。我無法在我的設備上重現您的問題。你能分享你的代碼的相關部分嗎?特別是我想測試使用你的地圖初始化(移動相機,等等......) – antonio

1

寫下的基本代碼來創建與色彩效果的多邊形。

private PolygonOptions mPolygonOptions; 
private GoogleMap mGoogleMap; 
private SupportMapFragment mMapFragment; 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mPolygonOptions = new PolygonOptions(); 
    } 
@Override 
    protected void onResume() { 
     super.onResume(); 
     if(mGoogleMap == null) { 
      mMapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.locationtrackmap)); 
      mGoogleMap = mMapFragment.getMap(); 
      mPolygonOptions.add(new LatLng(latitudeLongitude.One,latitudeLongitude.One)); 
      mPolygonOptions.add(new LatLng(latitudeLongitude.Two,latitudeLongitude.Two)); 
      mPolygonOptions.add(new LatLng(latitudeLongitude.Three,latitudeLongitude.Three)); 
      mPolygonOptions.add(new LatLng(latitudeLongitude.One,latitudeLongitude.One)); 
      mPolygonOptions.strokeColor(Color.RED); 
      mPolygonOptions.strokeWidth(5); 
      mPolygonOptions.fillColor(0x220000FF); 
      mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLon_to_focus, 14.0f)); 
       mGoogleMap.addPolygon(mPolygonOptions);} 
    }