5

I am new to OSM and OSMdroid.

I was following this pretty good tutorial to show offline maps. So basically what I have done is:

  • Created a tile package in zip format with Mobile Atlas Creator
  • Used MapQuest source, JPEG format
  • Put the zip into the right folder: /mnt/sdcard/osmdroid/

The problem was the tiles were not rendered. I got a blank page.

I found this solution, to solve my problem.

But now, it is bothering me that I have to use PNG files, that takes significantly more space. It is not really efficient for my app because the user will have to download a much larger package.

MY QUESTION IS: How can I use JPEG tiles with OSMDroid and MapQuest?

Thanks in advance.

raver99
  • 99
  • 1
  • 4

3 Answers3

3

This works to get JPGs instead of PNGs:

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/" }));

Notice ".jpg" in line 3.

Sorcerer
  • 854
  • 1
  • 8
  • 20
1

I created a tile source that suppoort jpg, you can take a look and adapt your case, Please note that getTileRelativeFilenameString won't contain .title ext. That part will be added by (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);
    }

}
Tran Hoang Duy Anh
  • 229
  • 1
  • 2
  • 13
  • If this is not working for some, check that mName in the MapTilerCustomDataSource() constructor matches the name of the topmost folder in your zipfile. – Martin Epsz Apr 25 '14 at 14:36
0

Download 'xxx.JPG.tile' files and rename them to 'xxx.PNG.tile'.

  • @vonbrand - I've downloaded .jpg tiles and wasn't able to see the maps. After renaming those files to .png.tile, it started working - so tiles "stayed" in JPEG format as requested. – flamencoman Jan 30 '14 at 22:23