【问题标题】:OsmDroid and MapQuest: How can I use JPEG tiles?OsmDroid 和 MapQuest:如何使用 JPEG 瓦片?
【发布时间】:2013-02-20 09:20:53
【问题描述】:

我是OSMOSMdroid 的新手。

我正在关注这个相当不错的tutorial 来显示离线地图。所以基本上我所做的是:

  • 使用 Mobile Atlas Creator 创建了一个 zip 格式的切片包
  • 使用 MapQuest 源,JPEG 格式
  • 将压缩包放入正确的文件夹:/mnt/sdcard/osmdroid/

问题是瓷砖没有被渲染。我有一个空白页。

我找到了this solution,来解决我的问题。

但现在,我不得不使用 PNG 文件,这让我很困扰,这会占用更多空间。这对我的应用来说效率并不高,因为用户必须下载一个更大的包。

我的问题是:如何在 OSMDroid 和 MapQuest 中使用 JPEG 图块?

提前致谢。

【问题讨论】:

  • 没有答案?我也有同样的问题。一切正常,但我必须使用 png,这会使文件更大。希望能够使用 jpeg 文件。
  • 请看下面的任何答案。您甚至可以修改代码以适应更多格式

标签: android maps offline osmdroid mapquest


【解决方案1】:

这可以获取 JPG 而不是 PNG:

MapView myOpenMapView;
myOpenMapView = (MapView) findViewById(R.id.openmapview);
myOpenMapView.setTileSource(new XYTileSource("MapquestOSM", ResourceProxy.string.mapquest_osm, 0, 18, 256, ".jpg", new String[] {
                "http://otile1.mqcdn.com/tiles/1.0.0/map/", "http://otile2.mqcdn.com/tiles/1.0.0/map/", "http://otile3.mqcdn.com/tiles/1.0.0/map/",
                "http://otile4.mqcdn.com/tiles/1.0.0/map/" }));

注意第 3 行中的“.jpg”。

【讨论】:

    【解决方案2】:

    我创建了一个支持 jpg 的图块源,您可以查看并调整您的案例, 请注意 getTileRelativeFilenameString 不会包含 .title 分机。该部分将由(MapTileFilesystemProvider)添加

    import java.io.File;
    import java.io.InputStream;
    import java.util.Random;
    
    import org.osmdroid.ResourceProxy;
    import org.osmdroid.ResourceProxy.string;
    import org.osmdroid.tileprovider.MapTile;
    import org.osmdroid.tileprovider.tilesource.BitmapTileSourceBase.LowMemoryException;
    import org.osmdroid.tileprovider.tilesource.ITileSource;
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    
    public class MapTilerCustomDataSource implements ITileSource {
    
        private static int globalOrdinal = 0;
    
        private final int mMinimumZoomLevel;
        private final int mMaximumZoomLevel;
    
        private final int mOrdinal;
        protected final String mName;
        protected final String mImageFilenameEnding;
        protected final Random random = new Random();
    
        private final int mTileSizePixels;
    
        private final string mResourceId;
    
        public MapTilerCustomDataSource() {
            mResourceId = null;
            mOrdinal = globalOrdinal++;
            mName = "MapquestOSM";
            mMinimumZoomLevel = 0;
            mMaximumZoomLevel = 20;
            mTileSizePixels = 256;
            mImageFilenameEnding = ".jpg";
    
        }
    
        @Override
        public String getTileRelativeFilenameString(final MapTile tile) {
            final StringBuilder sb = new StringBuilder();
            sb.append(pathBase());
            sb.append('/');
            sb.append(tile.getZoomLevel());
            sb.append('/');
            sb.append(tile.getX());
            sb.append('/');
            sb.append(tile.getY());
            sb.append(imageFilenameEnding());
            return sb.toString();
        }
    
        @Override
        public Drawable getDrawable(String aFilePath) throws LowMemoryException {
            try {
                // default implementation will load the file as a bitmap and create
                // a BitmapDrawable from it
                final Bitmap bitmap = BitmapFactory.decodeFile(aFilePath);
                if (bitmap != null) {
                    return new BitmapDrawable(bitmap);
                } else {
                    // if we couldn't load it then it's invalid - delete it
                    try {
                        new File(aFilePath).delete();
                    } catch (final Throwable e) {
                    }
                }
            } catch (final OutOfMemoryError e) {
                System.gc();
            }
            return null;
    
        }
    
        @Override
        public Drawable getDrawable(InputStream aFileInputStream) throws LowMemoryException {
            try {
                // default implementation will load the file as a bitmap and create
                // a BitmapDrawable from it
                final Bitmap bitmap = BitmapFactory.decodeStream(aFileInputStream);
                if (bitmap != null) {
                    return new BitmapDrawable(bitmap);
                }
            } catch (final OutOfMemoryError e) {
                System.gc();
            }
            return null;
    
        }
    
        @Override
        public int ordinal() {
            return mOrdinal;
        }
    
        @Override
        public String name() {
            return mName;
        }
    
        public String pathBase() {
            return mName;
        }
    
        public String imageFilenameEnding() {
            return mImageFilenameEnding;
        }
    
        @Override
        public int getMinimumZoomLevel() {
            return mMinimumZoomLevel;
        }
    
        @Override
        public int getMaximumZoomLevel() {
            return mMaximumZoomLevel;
        }
    
        @Override
        public int getTileSizePixels() {
            return mTileSizePixels;
        }
    
        @Override
        public String localizedName(final ResourceProxy proxy) {
            return proxy.getString(mResourceId);
        }
    
    }
    

    【讨论】:

    • 如果这对某些人不起作用,请检查 MapTilerCustomDataSource() 构造函数中的 mName 是否与 zipfile 中最顶层文件夹的名称匹配。
    【解决方案3】:

    下载“xxx.JPG.tile”文件并将其重命名为“xxx.PNG.tile”。

    【讨论】:

    • @vonbrand - 我下载了 .jpg 拼贴图,但看不到地图。将这些文件重命名为 .png.tile 后,它开始工作 - 因此图块按照要求“保持”为 JPEG 格式。
    猜你喜欢
    • 2014-02-19
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    相关资源
    最近更新 更多