Вопрос

I would like to use the google maps api v2, but show just a dummy map in the background. This is a sample of the PNG file I am using, called "dummy_map_tile.png". I placed it in the asset folder, under a dir named "images". It's size is 256x256 pixels. Tried also a JPG similar file.

My dummy map tile:

This is the code for my dummy map tile provider, which of course is supposed to work offline:

public class DummyTileProvider implements TileProvider {

    protected Tile mDummyTile = null;

    public DummyTileProvider(Context context) {
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        try {
            String tileFilename = "images/dummy_map_tile.png";
            inputStream = context.getResources().getAssets().open(tileFilename);
            outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[2048];
            int count;
            while((count = inputStream.read(buffer)) != -1)
                outputStream.write(buffer, 0, count);
            outputStream.flush();
            mDummyTile = new Tile(256, 256, outputStream.toByteArray());
        }
        catch (IOException e) {
            mDummyTile = null;
        }
        finally {
            if (inputStream != null)
                try {inputStream.close();} catch (IOException e) {}
            if (outputStream != null)
                try {outputStream.close();} catch (IOException e) {}
        }
    }

    @Override
    public Tile getTile(int x, int y, int zoom) {
        return mDummyTile;
    }
}

Some logging (not shown in the code above) allowed me to make sure that the dummy tile provider constructs properly, i.e. no IOException occurs, and mDummyTile is not null.

This is the way I am setting the tile provider in the map setup (mMap is my GoogleMap object, properly initialized):

    mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
    DummyTileProvider tileProvider = new DummyTileProvider(this);
    mMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));

Unfortunately, the map doesn't show at all. The getTile method is never called. All markers and other stuff I am drawing on the map work correctly, though. If I remove the three lines of code above, thus using the default tile provider, all works perfectly, showing the standard google maps (only in online mode). Can anyone give me a useful hint?

Это было полезно?

Решение

There is nothing wrong with the tile provider. It was just a mistake in the rest of the code, that happened to call method clear() on the map object, thus removing the tile provider from the map. Nevertheless, I hope this example of a dummy tile provider can be useful, so I'll leave it here.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top