我要实现具有以下功能的TileProvider:Map
请求aTile
TileProvider检查是否说Tile
可以脱机使用
如果是,tileprovider返回saidTile
无论可用性如何,TileProvider
都会联机检查新磁贴并将其下载到外部存储中的Cache
中。TileProvider
为地图提供新的平铺,地图会对其进行更新。
这是我当前的UrlTileProvider
实现:
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;
}
}
}
但是,这只会将其缓存在内存中,因此如果用户关闭我的应用程序,它不会缓存它们,并且默认的google地图将在下载
Tiles
之前显示。UrlTileProvider
将getTile
实现为final
,因此无法覆盖它。TileProvider
没有getTileURL
方法,UrlTileProvider
源代码不可用于重写我的解决方案。我该如何处理?有什么办法做我想做的吗?为什么
getTile
是最终的?我遗漏了什么吗? 最佳答案
用第二个类解决了这个问题,这个类包装在第一个类上。
尽管我不喜欢包装纸,但这是我能想到的最好的方法,我必须说它工作得很好。
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;
}
}
这样,为了让
TileProvider
工作,我只需要这样做: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);
我知道我的bytearrayfrompoutstream效率极低。欢迎升级我的代码。