Google Maps API Android - 平铺提供商的平铺分辨率

时间:2014-08-09 12:55:39

标签: android google-maps resolution tile

我在Android应用中使用自定义TileProviders来显示离线地图和OpenStreetMap地图。它有效,但是瓷砖分辨率存在问题,这是非常糟糕的。这些文件的大小为256x256,将我的TileProvider的宽度/高度设置为128并不会改变任何内容。

以下是一段代码:

public class GenericUrlTileProvider extends UrlTileProvider {

    // ---------------------------------------------------------------------------------------
    // Private attributes :

    private String _baseUrl;
    // ---------------------------------------------------------------------------------------



    // ---------------------------------------------------------------------------------------
    // Constructor :
    public GenericUrlTileProvider(int width, int height, String url) {

        super(width, height);

        this._baseUrl = url;
    }

    @Override
    public URL getTileUrl(int x, int y, int zoom) {

        try {
            return new URL(_baseUrl.replace("{z}", "" + zoom).replace("{x}", "" + x).replace("{y}", "" + y));
        }
        catch (MalformedURLException e) { e.printStackTrace(); }

        return null;
    }
    // ---------------------------------------------------------------------------------------
}

有谁知道如何解决此问题以支持高分辨率设备?

由于

1 个答案:

答案 0 :(得分:2)

在@grub请求中,这是我为获得下一个缩放级别的4个区块所做的工作:

public Tile getTileFromNextZoomLevel(int x, int y, int zoom) {

    final String topLeftTileUrl = _source.getUrlSchema().replace("{z}", "" + (zoom + 1)).replace("{x}", "" + (x * 2)).replace("{y}", "" + (y * 2));
    final String topRightTileUrl = _source.getUrlSchema().replace("{z}", "" + (zoom + 1)).replace("{x}", "" + (x * 2 + 1)).replace("{y}", "" + (y * 2));
    final String bottomLeftTileUrl = _source.getUrlSchema().replace("{z}", "" + (zoom + 1)).replace("{x}", "" + (x * 2)).replace("{y}", "" + (y * 2 + 1));
    final String bottomRightTileUrl = _source.getUrlSchema().replace("{z}", "" + (zoom + 1)).replace("{x}", "" + (x * 2 + 1)).replace("{y}", "" + (y * 2 + 1));

    final Bitmap[] tiles = new Bitmap[4];

    Thread t1 = new Thread() {

        @Override
        public void run() { tiles[0] = Utils.getBitmapFromURL(topLeftTileUrl); }
    };
    t1.start();

    Thread t2 = new Thread() {

        @Override
        public void run() { tiles[1] = Utils.getBitmapFromURL(topRightTileUrl); }
    };
    t2.start();

    Thread t3 = new Thread() {

        @Override
        public void run() { tiles[2] = Utils.getBitmapFromURL(bottomLeftTileUrl); }
    };
    t3.start();

    Thread t4 = new Thread() {

        @Override
        public void run() { tiles[3] = Utils.getBitmapFromURL(bottomRightTileUrl); }
    };
    t4.start();

    try {
        t1.join();
        t2.join();
        t3.join();
        t4.join();
    }
    catch (InterruptedException e) { e.printStackTrace(); }

    byte[] tile = Utils.mergeBitmaps(tiles, Bitmap.CompressFormat.JPEG); // PNG is a lot slower, use it only if you really need to

    return tile == null ? TileProvider.NO_TILE : new Tile( (int) _source.getTileSize().getWidth(), (int) _source.getTileSize().getHeight(), tile);
}

和Utils方法:

public static byte[] mergeBitmaps(Bitmap[] parts, Bitmap.CompressFormat format) {

    // Check if all the bitmap are null (if so return null) :
    boolean allNulls = true;
    for (int i = 0; i < parts.length; i++) {

        if(parts[i] != null) {

            allNulls = false;
            break;
        }
    }
    if(allNulls) return null;

    Bitmap tileBitmap = Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(tileBitmap);
    Paint paint = new Paint();
    for (int i = 0; i < parts.length; i++) {

        if(parts[i] == null) {

            parts[i] = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
        }
        canvas.drawBitmap(parts[i], parts[i].getWidth() * (i % 2), parts[i].getHeight() * (i / 2), paint);
    }

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    tileBitmap.compress(format, 100, stream);
    byte[] bytes = stream.toByteArray();

    return bytes;
}

public static Bitmap getBitmapFromURL(String urlString) {

    try {

        // Ensure the file exists :
        if(Utils.getResponseCode(urlString) != 200) return null;

        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        Bitmap bitmap = BitmapFactory.decodeStream(connection.getInputStream());

        return bitmap;
    }
    catch (IOException e) { return null; }
}

您可能需要根据自己的需要进行调整。请注意我的应用程序仍在开发中,此代码可能需要一些测试/改进。