Reputation: 291
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: https://i.sstatic.net/CoZT1.png
Upvotes: 4
Views: 5888
Reputation: 69
I also had problems getting this to work. My setup is offline Osmdroid with Mapnik. It's now working. The code I'm using is as follows.
Firstly you need to include OSMDroid in build.gradle (Module:app)
implementation 'org.osmdroid:osmdroid-android:6.1.5'
Do a Gradle sync after you add this line.
My main activity is like this:
public class MainActivity extends AppCompatActivity {
MapView map = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
map = findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
map.setUseDataConnection(false);
map.setMinZoomLevel(7.0);
map.setMaxZoomLevel(18.0);
IMapController mapController = map.getController();
mapController.setZoom(9.0);
map.invalidate();
GeoPoint goToLocation = new GeoPoint(41.80937753568571, -5.739056020777395);
mapController.setCenter(goToLocation);
}
}
The activity .xml contains the view:
<org.osmdroid.views.MapView android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
tilesource="Mapnik" />
My .zip file with the map tiles is in /storage/emulated/0/osmdroid
(I could not get this to work from location /sdcard/osmdroid)
The internal structure of your .zip file should be /Mapnik/x/y/z.png This is CASE SENSITIVE!
It doesn't seem to matter what your .zip file is called and you can even have more than one of them.
After you compile and install for the first time you may have to go into Application Properties on Android and grant the app access to "Storage" (not sure of the exact translation, my phone is in Spanish and it's called "Almacenamiento").
Good luck!
Upvotes: 0
Reputation: 1333
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
Upvotes: 2
Reputation: 9761
Your code is right MapTileProviderBasic use MapTileFileArchiveProvider:
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/...
Upvotes: 1