Osmdroid offline file

2019-06-24 05:16发布

问题:

I have an osmdroid map, and i want to load offline tiles from either MOBAC's osmdroid zip or gemf file. I looked into it and everywhere i go it says just put it in sdcard/osmdroid. But it doesnt work, do i need to change something in the code?

 public class Map extends Activity {
  private IMapView mMapView;
  private static IMapController mMapController;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    mMapView = (MapView) findViewById(R.id.mapview);
    ((MapView) mMapView).setBuiltInZoomControls(true);

    mMapController = mMapView.getController();
    mMapController.setZoom(13);
    GeoPoint geoPecs = new GeoPoint(46.070833,18.233056);
    mMapController.setCenter(geoPecs);

mProvider = new MapTileProviderBasic(getApplicationContext());
    mCustomTileSource = new XYTileSource("Turaterkep", null, 13, 15, 256, ".png", "http://users.atw.hu/perzsa/map/Turaterkep/");
    mProvider.setTileSource(mCustomTileSource);
    mTilesOverlay = new TilesOverlay(mProvider,this.getBaseContext());

    mMapView.getOverlays().add(mTilesOverlay);

update: I managed to load the tiles from online with the samples included to osmdroid, i updated the code as well, i guess i have to start with this sample, maybe i can load from localhost? ill check back

picture: http://i.stack.imgur.com/CoZT1.png

回答1:

I experienced the same problem and I solved it by using online tiles from Mapnik. This way, my application created the path in my mobile device (phone/osmdroid/tiles/Mapnik).

    mMapView.setTileSource(TileSourceFactory.Mapnik);

You can also add the tilesouce by adding this to your xml-file and in your mapview:

    tilesource="Mapnik"

Dont forget to specify that you don't want the application to download data by adding:

      mMapView.setUseDataConnection(false);

With that done, you can get the application to use offline tiles, just follow this guide: http://www.haakseth.com/?p=30



回答2:

Your code is right MapTileProviderBasic use MapTileFileArchiveProvider:

https://code.google.com/p/osmdroid/source/browse/trunk/osmdroid-android/src/org/osmdroid/tileprovider/MapTileProviderBasic.java?r=681

You should verify the path of your zip and the directory structure in your zip file.

The zip file should be placed into OSMDROID_PATH (/mnt/sdcard/osmdroid). That's where the zip files are searched:

private void findArchiveFiles() {

    mArchiveFiles.clear();

    if (!getSdCardAvailable()) {
            return;
    }

    // path should be optionally configurable
    final File[] z = OSMDROID_PATH.listFiles();
    for (final File file : z) {
            final IArchiveFile archiveFile = ArchiveFileFactory.getArchiveFile(file);
            if (archiveFile != null) {
                    mArchiveFiles.add(archiveFile);
            }
    }
}

Inside your zip don't forget to set the tile source name as base directory:

/tilesourcename/zoomlevel/...