7

I am developing an application in which I need to use maps offline. I'm using osmdroid and osmbonuspack

To download the maps I've tried:

For me the ideal would be to download the maps from the application itself, and I just want to download the maps you are on a track, not a complete section.

How could I fix it?

Is there any way to download maps from the phone through my application?

user3292741
  • 73
  • 1
  • 1
  • 4

4 Answers4

23

Working solution with MobileAtlasCreator / MOBAC:

There is an osmdroid documentation, but it is very weak, and sometimes outdated.

I struggled for a while on successive issues. Here are the details of a working solution with osmdroid v4.1.

1) When building your offline map with MOBAC:

  • As Mapnik maps are effectively locked, select "OpenStreetMap MapQuest" as the source.
  • Atlas Format: choose "Osmdroid ZIP"
  • Take care to tick all zoom levels you will need. By default, none are selected.

Select your area, create your "atlas". This produces a zip file.

Upload the zip file on your device, in /sdcard/osmdroid/ (exact path may vary depending on the device. If you already used osmdroid, this directory MUST already exist)

The file name doesn't matter. The extension MUST be ".zip"

2) Now, in your osmdroid application, your onCreate method should be something like this:

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map = (MapView) findViewById(R.id.map);
    map.setTileSource(new XYTileSource("MapQuest",
        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/"}));
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);
    map.setUseDataConnection(false); //optional, but a good way to prevent loading from the network and test your zip loading. 
    IMapController mapController = map.getController();
    mapController.setZoom(_A ZOOM LEVEL YOU HAVE IN YOUR ZIP_);
    GeoPoint startPoint = new GeoPoint(_POSITION SOMEWHERE INSIDE YOUR MAP_);
    mapController.setCenter(startPoint);
}

In this code, 2 parameters values are VERY important:

"MapQuest" name (with this EXACT spelling) is MANDATORY => this is used as an internal path inside the zip file. If you open your zip file, you will see that MOBAC created this "MapQuest" directory.

".jpg" extension is also MANDATORY => as MOBAC creates MapQuest tiles in the zip with .jpg extension (important to notice, as standard tile sources in osmdroid are all using ".png" extension).

At this stage, it should be OK - as long as you are really positionning the mapview on an area which is part of your atlas (zoom level & position).

3) Back to MOBAC... You can also choose the following Atlas formats: "Osmdroid SQLite" or "MBTiles SQLite". Transfert the file (Layer.sqlite or Layer.mbtiles) on the device in /sdcard/osmdroid/

Again, in your XYTileSource constructor, the extension MUST be ".jpg". The name doesn't matter.

Both worked fine.

4) Choosing "Osmdroid GEMF" format will not work: it's a known bug in GEMF on handling of jpg tiles. EDIT > In MOBAC, you can use the "custom tile processing" feature to convert JPG tiles to PNG format. Then "Osmdroid GEMF" will be ok.

MKer
  • 3,430
  • 1
  • 13
  • 18
  • **MKer** Thanks for the reply. But I have a problem. If I put `map.setUseDataConnection(true)` it doesn't download maps. How can I do to download the map if not exist? – user3292741 Apr 06 '14 at 11:14
  • The issue is that setting .jpg extension is required for the MapQuest zip file, but doesn't work with openstreetmap.org online tiles (which are .png files). So we must also use MapQuest for online tiles. I edit the code to change that. – MKer Apr 06 '14 at 21:11
  • Nice explanation. Do you have any code for offline tile source to work? the above code works for online only! – Muhammad Babar Sep 15 '14 at 04:45
  • The code above is dedicated to offline tiles: map.setUseDataConnection(false) is here to ensure that online tiles are not retrieved. And it should work. If not, describe your use case/context with more details. – MKer Sep 15 '14 at 08:45
  • Are you serious? `http://otile1.mqcdn.com/tiles/1.0.0/map/` this is web call and require internet. I don't see any code that is used for reading the map from internal/external storage? – Muhammad Babar Sep 23 '14 at 05:04
  • 1
    Yes, I'm serious. You can read the osmdroid source code if you don't trust me, and an overview of osmdroid loading strategy can be found here: https://code.google.com/p/osmdroid/wiki/ModularTileProviderArchitecture – MKer Sep 23 '14 at 07:24
  • Is there anyway I could make the offline maptile provider look at a folder on a 64GB SD card inserted into the phone? This way I could store a lot of map data – Paul Alexander Apr 21 '15 at 11:27
  • osmdroid wiki docs is now here: https://github.com/osmdroid/osmdroid/wiki/Offline-Map-Tiles – spy Jan 02 '17 at 14:36
5

About to the second - preferred - approach asked in the question: "Is there any way to download maps from the phone through my application?"

=> Answer is now: Yes!

From OSMBonusPack v4.6, there is a CacheManager class, allowing to download the tiles of a (rectangular) area, directly from the Android application. Tiles are loaded in the standard osmdroid tiles cache.

It can be tested using OSMNavigator.

MKer
  • 3,430
  • 1
  • 13
  • 18
4

First of all Thank you MKer and to Tom Kincaid from another post, putting the two together. The following works for me:

This was what I needed to do that's different from MKer:

    String[] urls = {"http://127.0.0.1"};
    mapView.setTileSource(new XYTileSource("MapQuest", // name of the main file that has been zipped
            ResourceProxy.string.mapquest_osm, 
            2,        // min map zoom level
            14,       // max map zoom level
            256,      // tile size pixels
            ".jpg",   // extension of the tiles (can be ".png" as well)
            urls));

After creating the maps using Mobile Atlas Creator

user2010136
  • 141
  • 1
  • 7
0

Today just OSM Map Tile Packager works for map tile mapnik and it do maps in PNG, so

 if(ConexaoInternet.verificaConexao(getActivity())) { //<-- here i am Checking if has conection with internet
        mapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
    }else{
        String[] urls = {"http://127.0.0.1"};
        mapView.setTileSource(new XYTileSource("Mapnik", // name of the main file that has been zipped
                ResourceProxy.string.mapquest_osm,
                9,        // min map zoom level
                15,       // max map zoom level
                256,      // tile size pixels
                ".png",
                new String[]{"http://a.tile.openstreetmap.org/", "http://b.tile.openstreetmap.org/", "http://c.tile.openstreetmap.org/"}));
    }

I prefer put some urls concrete.