So I\'ve finally gotten osmdroid working with a local directory but I\'d like to load tiles from Mapnik when they are missing locally. I\'m not sure what I\'m missing.
Okay so I've finally found a solution. Its quite complex and long but it works :D
Now inside my OsmDroidFragment (yes map in fragment) I have following:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.osm_map, container, false);
RelativeLayout mapContainer = (RelativeLayout) view.findViewById(R.id.osm_map_parent);
mMapView = new OsmCustomMapView(getActivity(), 256, 10, 13); // I've made a custom implementation in order to limit zoom, you can just use regular osmdroid mapview
LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mMapView.setLayoutParams(params);
mapContainer.addView(mMapView);
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(false); // pinch zoom works bad on osmdroid
MapController mapController = mMapView.getController();
mapController.setZoom(11); // must set zoom before setCenter, else we get the wrong position
GeoPoint center = new GeoPoint(0,0);
mapController.setCenter(center);
// save zip to sd
AssetManager assetManager = getActivity().getAssets();
InputStream is;
String fileName = "map.zip"; // the zip file lies in assets root
String path = this.getActivity().getExternalFilesDir(null) + File.separator + fileName; // the path I save SD to
File tileFile = new File(path);
if(!tileFile.exists()) {
try {
is = assetManager.open(fileName);
FileOutputStream fo = new FileOutputStream(path);
byte[] b = new byte[1024];
int length;
while((length = is.read(b)) != -1) {
fo.write(b, 0, length);
}
fo.flush();
fo.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
IArchiveFile[] archives = new IArchiveFile[1];
archives[0] = ArchiveFileFactory.getArchiveFile(tileFile);
// Simple implementation that extends BitmapTileSourceBase and nothing else
CustomTileSource customTiles = new CustomTileSource("Maverik", null, 10, 14, 256, ".png"); // Maverik is the name of the folder inside the zip (so zip is map.zip and inside it is a folder called Maverik)
MapTileModuleProviderBase[] providers = new MapTileModuleProviderBase[2];
providers[0] = new MapTileFileArchiveProvider(new SimpleRegisterReceiver(getActivity().getApplicationContext()), customTiles, archives); // this one is for local tiles (zip etc.)
providers[1] = new MapTileDownloader(TileSourceFactory.MAPNIK); // MAPNIK web tile source
mMapView.setUseDataConnection(true);
MapTileProviderArray tileProvider = new MapTileProviderArray(customTiles,
new SimpleRegisterReceiver(getActivity().getApplicationContext()), providers);
TilesOverlay tilesOverlay = new TilesOverlay(tileProvider, getActivity().getApplicationContext());
tilesOverlay.setLoadingBackgroundColor(Color.TRANSPARENT); // this makes sure that the invisble tiles of local tiles are transparent so we can see tiles from web, transparent have a minor performance decline.
mMapView.getOverlays().add(tilesOverlay);
mMapView.invalidate();
return view;
}