首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在osmdroid MapView中使用离线地图数据时,渲染速度非常慢

在osmdroid MapView中使用离线地图数据时,渲染速度非常慢
EN

Stack Overflow用户
提问于 2013-08-12 16:55:40
回答 2查看 914关注 0票数 0

我的离线地图数据源格式是MBTiles。数据库有两个表: Table1 name是preferences,它存储的数据有: name,value

Table2名称是存储数据的磁贴,如下所示:

tileKey,缩放,行,列,图像

然后我创建了一个类

公共类MBTileSource扩展了BitmapTileSourceBase {

代码语言:javascript
复制
// Log log log log ...
private static final Logger logger = LoggerFactory
        .getLogger(MBTileSource.class);

// Database related fields
public final static String TABLE_TILES = "tiles";
public final static String COL_TILES_ZOOM_LEVEL = "zoom";
public final static String COL_TILES_TILE_COLUMN = "col";
public final static String COL_TILES_TILE_ROW = "row";
public final static String COL_TILES_TILE_DATA = "image";

protected SQLiteDatabase database;
protected File archive;

// Reasonable defaults ..
public static final int minZoom = 8;
public static final int maxZoom = 15;
public static final int tileSizePixels = 256;

// Required for the superclass
public static final string resourceId = ResourceProxy.string.offline_mode;

/**
 * The reason this constructor is protected is because all parameters,
 * except file should be determined from the archive file. Therefore a
 * factory method is necessary.
 * 
 * @param minZoom
 * @param maxZoom
 * @param tileSizePixels
 * @param file
 */
protected MBTileSource(int minZoom, int maxZoom, int tileSizePixels,
        File file, SQLiteDatabase db) {
    super("sqlite", resourceId, minZoom, maxZoom, tileSizePixels, ".png");

    archive = file;
    database = db;
}

/**
 * Creates a new MBTileSource from file.
 * 
 * Parameters minZoom, maxZoom en tileSizePixels are obtained from the
 * database. If they cannot be obtained from the DB, the default values as
 * defined by this class are used.
 * 
 * @param file
 * @return
 */
public static MBTileSource createFromFile(File file) {
    SQLiteDatabase db;
    int flags = SQLiteDatabase.NO_LOCALIZED_COLLATORS
            | SQLiteDatabase.OPEN_READONLY;

    // int value;
    int minZoomLevel=minZoom;
    int maxZoomLevel=maxZoom;
    int tileSize = tileSizePixels;
    InputStream is = null;

    // Open the database
    db = SQLiteDatabase.openDatabase(file.getAbsolutePath(), null, flags);

    // Get the minimum zoomlevel from the MBTiles file
    Cursor pCursor = db.rawQuery("SELECT * FROM preferences;", null);
    try {
        if (pCursor.getCount() != 0) {
            pCursor.moveToFirst();
            do {
                String name=pCursor.getString(pCursor.getColumnIndex("name"));
                if(name.equalsIgnoreCase("map.minZoom")){
                    minZoomLevel=pCursor.getInt(pCursor.getColumnIndex("value"));
                }else if(name.equalsIgnoreCase("map.maxZoom")){
                    maxZoomLevel=pCursor.getInt(pCursor.getColumnIndex("value"));
                }else if(name.equalsIgnoreCase("map.tileSideLength")){
                    tileSize=pCursor.getInt(pCursor.getColumnIndex("value"));
                }
            } while (pCursor.moveToNext());
        }
        pCursor.close();
    } catch (Exception e) {
        // TODO: handle exception
    }

    return new MBTileSource(minZoomLevel, maxZoomLevel, tileSize, file, db);
}

public InputStream getInputStream(MapTile pTile) {

    try {
        InputStream ret = null;
        final String[] tile = { COL_TILES_TILE_DATA };
        final String[] xyz = {
                Integer.toString(pTile.getX()),
                Double.toString(Math.pow(2, pTile.getZoomLevel())
                        - pTile.getY() - 1),
                Integer.toString(pTile.getZoomLevel()) };

        final Cursor cur = database.query(TABLE_TILES, tile,
                "col=? and row=? and zoom=?", xyz, null,
                null, null);

        if (cur.getCount() != 0) {
            cur.moveToFirst();
            ret = new ByteArrayInputStream(cur.getBlob(0));
        }

        cur.close();

        if (ret != null) {
            return ret;
        }

    } catch (final Throwable e) {
        logger.warn("Error getting db stream: " + pTile, e);
    }

    return null;

}

}

它可以很好地显示离线地图。唯一的问题是:渲染速度非常慢。

有什么建议吗?谢谢。

EN

回答 2

Stack Overflow用户

发布于 2013-08-12 20:29:59

您不希望扩展BitmapTileSourceBase。您希望创建一个包含MapTileFileArchiveProvider和您自己的IArchiveFile的瓦片提供商链。有关数据库支持的IArchiveFile的示例,请参阅DatabaseFileArchive.java

如果你仍然有速度问题,你应该运行分析器,看看哪里慢下来了。

票数 1
EN

Stack Overflow用户

发布于 2013-10-11 03:55:07

你的mbtile文件有任何索引吗?Kurtzmarc的答案将会起作用,但在这条道路上会变得一团糟。我已经让它工作了,性能也不错,但我想要一个更干净的解决方案。我正在考虑从XYTileSource开始,然后从那里开始。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18182969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档