A dummy map tile provider not working (getTile not being called)

耗尽温柔 提交于 2019-12-23 15:59:24

问题


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.

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?


回答1:


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.



来源:https://stackoverflow.com/questions/21165489/a-dummy-map-tile-provider-not-working-gettile-not-being-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!