android - 谷歌地图添加色调

标签 android google-maps

我想对两个标记周围的 GoogleMaps 应用色调。到目前为止,我得到的唯一可行的解​​决方案是在 map 上实际绘制多边形

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 预览 map preview .有没有人可以帮助我改进现有方法或建议更好的方法来解决这个问题?

最佳答案

您可以创建一个 TileOverlay 并将其添加到 map (在您的标记上方或下方,具体取决于 zIndex)。

想法是创建一个 TileProvider,它始终返回一个非常小的(在我的示例中为 2x2 像素)图像,该图像将绘制在 map 上。为确保性能,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 添加到您的 map 中,如下所示:

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

关于android - 谷歌地图添加色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36264256/

相关文章:

Android setBackgroundResource 释放内存?

android - FAB 涟漪效应在视界之外持续存在

android - 搜索给定城市名称中的所有地点

javascript - 动态 jQuery 代码 : is there any way to include it in a file?

javascript - Cordova 背景地理定位不适用于背景

android - 如何使用微调器 setOnItemLongClickListener

android - 在 Android 中加载谷歌地图时出现问题

android - google map api V3 未在 android 模拟器上显示

android - Flutter 中项目复用后的问题

javascript - 你能在虚拟元素中创建谷歌地图吗