虚拟地图图块提供程序不起作用(未调用getTile)
我想使用google maps api v2,但仅在后台显示一个虚拟地图. 这是我正在使用的PNG文件的示例,称为"dummy_map_tile.png".我将其放置在资产文件夹中名为"images"的目录下.它的大小是256x256像素.还尝试了JPG相似文件.
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;
}
}
一些日志记录(上面的代码中未显示)使我能够确保虚拟图块提供程序的构造正确,即,没有IOException发生,并且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.
这是我在地图设置中设置图块提供程序的方式(mMap是我的GoogleMap对象,已正确初始化):
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));
不幸的是,地图根本没有显示. 永远不会调用getTile方法. 不过,我在地图上绘制的所有标记和其他内容均能正常工作. 如果我删除上面的三行代码,从而使用默认的图块提供程序,则所有显示效果都很好,显示了标准的Google地图(仅在在线模式下). 谁能给我有用的提示?
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?
磁贴提供程序没有任何问题.只是代码其余部分中的一个错误,碰巧在地图对象上调用方法clear()
,从而从地图上删除了图块提供程序.不过,我希望这个虚拟磁贴提供程序的示例会有用,所以我将其保留在此处.
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.