【问题标题】:"overzoom" tileoverlay google maps“过度缩放” tileoverlay 谷歌地图
【发布时间】:2017-09-19 22:49:22
【问题描述】:

我有一个问题,我目前正在尝试为其构建自定义解决方案。 我正在苦苦挣扎,也许我可以让它工作,但也许已经有另一种解决方案了。

我的客户有一个用于获取图块叠加层的后端,但它只能持续到缩放级别 8。在那之后,不会显示图块。

为了制作更多细节图块,我过去使用过https://stackoverflow.com/a/36696291/969016。它从更高的缩放级别获取 4 个图块并将其构造为 1 个。

但是现在,我必须从较低的缩放级别获取图块并且需要将其放大。我一直试图以上述为基础,但尚未成功。如果有人知道不同的方法,我将不胜感激。

或者,也许可以让 Google 地图保持缩放而不请求高于某个级别的新图层?我的意思是,它已经在缩放级别之间这样做了

【问题讨论】:

  • @YvetteColomb 不确定我需要添加哪些相关代码。基本的事情是,我需要能够使用 TileProvider 放大我没有瓷砖的缩放级别。我很害怕这是不可能的,但可能会证明我错了
  • 请注意:我让它工作了!清理完代码后我会发布解决方案!

标签: android google-maps


【解决方案1】:

以@RadekJ here 的提高图块分辨率的解决方案(通过将 4 个较高缩放级别的图块组合为 1 个)作为参考,我开始研究相反的方法:采用较低的缩放级别,将其切掉分成 4 个部分,并使用它来构建更高的缩放级别。

假设您获得的磁贴的最大缩放级别是 8,但您希望能够通过放大这些磁贴来缩小到 9、10 等。

每次放大时,图块都除以 2。所以以缩放级别 9 为例,我从缩放级别 8 中取出图块,并将其切成 4 块。然后我确定了第 9 层所请求的图块需要哪个“象限”。

接下来,我使用递归来获得更高级别的缩放级别。我对结果很满意。

如果您想查看带有xyzoom level 的图块,请将DRAW_DEBUG_DATA 设置为true

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.google.android.gms.maps.model.Tile;
import com.google.android.gms.maps.model.TileProvider;

import java.io.ByteArrayOutputStream;

public class OverZoomTileProvider implements TileProvider {

    public static final int MAX_ZOOM = 8;

    private static final boolean DRAW_DEBUG_DATA = false;

    private static final int TILE_SIZE = 256;
    private static final int HALF_TILE_SIZE = TILE_SIZE / 2;

    private Paint tilePainter = new Paint();

    // these will only be used when DRAW_DEBUG_DATA is true
    private Paint debugRectPaint;
    private Paint debugTextPaint;

    private TileProvider mTileProvider;

    public OverZoomTileProvider(TileProvider tileProvider) {
        mTileProvider = tileProvider;

        if (DRAW_DEBUG_DATA) {
            debugRectPaint = new Paint();
            debugRectPaint.setColor(Color.RED);
            debugRectPaint.setStrokeWidth(1);
            debugRectPaint.setStyle(Paint.Style.STROKE);

            debugTextPaint = new Paint();
            debugTextPaint.setColor(Color.WHITE);
            debugTextPaint.setStyle(Paint.Style.FILL);
            debugTextPaint.setColor(Color.BLACK);
            debugTextPaint.setTextSize(20);
        }
    }

    @Override
    public Tile getTile(int x, int y, int zoom) {
        Bitmap image = Bitmap.createBitmap(TILE_SIZE, TILE_SIZE,
                Bitmap.Config.ARGB_8888);
        image.eraseColor(Color.TRANSPARENT);

        Canvas canvas = new Canvas(image);
        drawTile(canvas, zoom, x, y);

        byte[] data = bitmapToByteArray(image);
        image.recycle();

        return new Tile(TILE_SIZE, TILE_SIZE, data);
    }

    private void drawTile(Canvas canvas, int zoom, int x, int y) {

        Bitmap bitmap = getTileAsBitmap(x, y, zoom);

        if (bitmap != null) {
            canvas.drawBitmap(bitmap, 0, 0, tilePainter);
            bitmap.recycle();
        }

        if (DRAW_DEBUG_DATA) {
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), debugRectPaint);
            canvas.drawText("" + x + ", " + x + " (" + zoom + ")", 128, 128, debugTextPaint);
        }
    }

    private Bitmap getTileAsBitmap(int x, int y, int zoom) {
        if (zoom <= MAX_ZOOM) {
            Tile tile = mTileProvider.getTile(x, y, zoom);

            if (tile == NO_TILE) {
                return null;
            }

            return BitmapFactory.decodeByteArray(tile.data, 0, tile.data.length);
        }


        boolean leftColumn = x % 2 == 0;
        boolean topRow = y % 2 == 0;

        Bitmap bitmap = getTileAsBitmap(x / 2, y / 2, zoom - 1);

        int quadrant;
        if (leftColumn && topRow) {
            quadrant = 1;
        } else if (!leftColumn && topRow) {
            quadrant = 2;
        } else if (leftColumn) {
            quadrant = 3;
        } else {
            quadrant = 4;
        }

        switch (quadrant) {
            case 1:
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, HALF_TILE_SIZE, HALF_TILE_SIZE);
                break;
            case 2:
                bitmap = Bitmap.createBitmap(bitmap, HALF_TILE_SIZE, 0, HALF_TILE_SIZE, HALF_TILE_SIZE);
                break;
            case 3:
                bitmap = Bitmap.createBitmap(bitmap, 0, HALF_TILE_SIZE, HALF_TILE_SIZE, HALF_TILE_SIZE);
                break;
            case 4:
                bitmap = Bitmap.createBitmap(bitmap, HALF_TILE_SIZE, HALF_TILE_SIZE, HALF_TILE_SIZE, HALF_TILE_SIZE);
                break;
        }

        return Bitmap.createScaledBitmap(bitmap, TILE_SIZE, TILE_SIZE, false);
    }

    private static byte[] bitmapToByteArray(Bitmap bm) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, bos);

        byte[] data = bos.toByteArray();
        try {
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 2012-09-08
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    相关资源
    最近更新 更多