【问题标题】:GoogleMaps add tint谷歌地图添加色彩
【发布时间】:2016-03-28 14:20:33
【问题描述】:

我想在两个标记周围的 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));
}

但是创建的叠加层并没有覆盖整个地图预览 。有没有人可以帮助我改进现有的方法或提出更好的方法来解决这个问题?

【问题讨论】:

  • 那么,你需要用半透明的颜色覆盖整个地图吗?

标签: android google-maps


【解决方案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));

【讨论】:

  • 感谢@antonio 这在某种程度上确实有效。根据我之前附加的屏幕截图,地图右侧现在已着色。然而,左侧有一条不会着色的条带。这似乎与animateCamera 之后的地图渲染有关。带有色调的地图的初始渲染很好,仅应用居中和缩放左侧会松散有色瓷砖。尽管如此,上面对我帮助很大。谢谢
  • 这很奇怪。我无法在我的设备上重现您的问题。你能分享你的代码的相关部分吗?特别是我想测试使用你的地图初始化(移动相机,等等......)
【解决方案2】:

编写基本代码以创建具有着色效果的多边形。

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);}
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多