I want to implement a TileProvider with this functionality:
Map
asks for aTile
- 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 theCache
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?