Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

android - Download maps for osmdroid

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...