5

I want to implement a TileProvider with this functionality:

  • Map asks for a Tile
  • TileProvider checks if said Tile is available offline
  • If so, TileProvider returns said Tile
  • Regardless of availability, TileProvider checks online for a new Tile and downloads it to the Cache in the External Storage.
  • TileProvider gives the map the new Tile, and the map updates it.

This is my current UrlTileProvider implementation:

public class MapBoxOnlineTileProvider extends UrlTileProvider {

    private static final String[] FORMATS;

    static {
        final String[] servers = new String[] { "a", "b", "c", "d" };
        final String[] formats = new String[servers.length];
        for (int i = 0; i < servers.length; i++) {
            formats[i] = String.format(
                    "%%s://%s.tiles.mapbox.com/v3/%%s/%%d/%%d/%%d.png",
                    servers[i]);
        }
        FORMATS = formats;
    }

    // ------------------------------------------------------------------------
    // Instance Variables
    // ------------------------------------------------------------------------

    private String mMapIdentifier;

    // ------------------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------------------



    public MapBoxOnlineTileProvider(final String mapIdentifier) {
        super(256, 256);
        this.mMapIdentifier = mapIdentifier;
    }

    // ------------------------------------------------------------------------
    // Public Methods
    // ------------------------------------------------------------------------

    public String getMapIdentifier() {
        return this.mMapIdentifier;
    }

    public void setMapIdentifier(final String anIdentifier) {
        this.mMapIdentifier = anIdentifier;
    }

    @Override
    public URL getTileUrl(final int x, final int y, final int z) {
        final String f = FORMATS[new Random().nextInt(FORMATS.length)];
        final String p = "http";
        try {
            return new URL(String.format(f, p, this.mMapIdentifier, z, x, y));
        } catch (final MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
    }
}

However, this will only cache it in memory, so if the user closes my app it won't cache them, and the default Google Maps map will appear before the Tiles are downloaded.

UrlTileProvider implements getTile as final, so I cannot overwrite it. TileProvider has no getTileURL method, and UrlTileProvider source code is not available for me to rewrite my solution.

How should I approach this? Is there any way to do what I want? Why is getTile final? Am I missing something?

1 Answer 1

1

Solved it with a second class which wraps on the first class.

As much as I dislike wrappers this was the best approach I could think of, and I must say it works pretty nicely.

public class MapBoxMixedTileProvider implements TileProvider {

    private final MapBoxOnlineTileProvider provider;
    private final Context context;

    public MapBoxMixedTileProvider(final Context context, final String mapId) {
        provider = new MapBoxOnlineTileProvider(mapId);
        this.context = context;
    }

    @Override
    public Tile getTile(final int arg0, final int arg1, final int arg2) {
        final URL url = provider.getTileUrl(arg0, arg1, arg2);
        // Remove the "http://a." from the url, then replace all the '/' and '.'
        // with
        // '_', then replace the final "_png" with ".png", so openFileInput
        // doesn't cry a lot.
        final String finalUrl = (url.toString().substring(9)).replace('/', '_')
                .replace('.', '_').replace("_png", ".png");
        System.out.println(finalUrl);
        boolean contained = false;
        for (final String file : context.fileList()) {
            if (file.contains(finalUrl)) {
                contained = true;
                break;
            }
        }
        try {
            if (contained) {
                final FileInputStream fis = context.openFileInput(finalUrl);
                return new Tile(256, 256, byteArrayFromInputStream(fis));
            } else {
                final FileOutputStream fos = context.openFileOutput(finalUrl,
                        Context.MODE_PRIVATE);
                final Tile t = provider.getTile(arg0, arg1, arg2);
                fos.write(t.data);
                fos.flush();
                fos.close();
                return t;
            }
        } catch (final IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    private byte[] byteArrayFromInputStream(final FileInputStream is)
            throws IOException {
        final BufferedInputStream bis = new BufferedInputStream(is);
        final ArrayList<Integer> bytes = new ArrayList<Integer>();
        int current = 0;
        while ((current = bis.read()) != -1) {
            bytes.add(Integer.valueOf(current));
        }
        final byte[] bs = new byte[bytes.size()];
        for (int i = 0; i < bytes.size(); i++) {
            bs[i] = bytes.get(i).byteValue();
        }
        bis.close();
        is.close();
        return bs;
    }

}

This way, in order to get the TileProvider to work, I only need to do this:

final MapBoxMixedTileProvider provider = new MapBoxMixedTileProvider(
                        this, MAPBOX_MAP_IDENTIFIER);
//zIndex is set to -4 so Polyines and Polygons get shown OVER and not under the Overlay
final TileOverlayOptions overlay = new TileOverlayOptions().tileProvider(provider).zIndex(-4);
tileOverlay = mMap.addTileOverlay(overlay);

I am aware that my byteArrayFromInputStream is EXTREMELY inefficient. Any upgrades to my code are welcome.

1
  • what is MapBoxOnlineTileProvider? Jan 7, 2015 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.