1

I am making a mobile application using osmdroid. I want to be able to use it online and offline. I use CacheManager to custom download maps by storing them in .sqlite files in the / osmdroid folder.

    public void downloadMap() {
        final SqliteArchiveTileWriter writer;
        createArea();
        String outputName = basePathMapTileProvider + area.name + ".sqlite";

        map.setTileSource(TileSourceFactory.OpenTopo);

        try {
            writer=new SqliteArchiveTileWriter(outputName);
            CacheManager cacheManager = new CacheManager(map,writer);
            //loadOfflineMap();

            cacheManager.downloadAreaAsync(ctx, area, zoomMin, zoomMax, new CacheManager.CacheManagerCallback() {
                @Override
                public void onTaskComplete() {
                    Toast.makeText(ctx, R.string.download_complete, Toast.LENGTH_LONG).show();
    //                loadOfflineMap();
                    if (writer!=null)
                        writer.onDetach();
                }
                @Override
                public void updateProgress(int progress, int currentZoomLevel, int zoomMin, int zoomMax) {

                }
                @Override
                public void downloadStarted() {
                    Toast.makeText(ctx, R.string.download_started, Toast.LENGTH_LONG).show();
                }
                @Override
                public void setPossibleTilesInArea(int total) {

                }
                @Override
                public void onTaskFailed(int errors) {
                    Toast.makeText(ctx, R.string.download_error, Toast.LENGTH_LONG).show();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

So far I can determine if a connection exists and use the default "provider" (MapTileProviderBasic) if there is a connection, or the OfflineTileProvider if there is not.

if (isNetworkAvaliable(this))
   loadOnlineMap();
else
   loadOfflineMap();

The problem is that if I use the default provider and there is no connection, it does not load the map stored in memory (.sqlite), only the one in the cache (osmdroid / tiles / cache.db).

    private void loadOnlineMap(){
        // Loading de main map
        map = findViewById(R.id.map);
        map.setTileSource(TileSourceFactory.OpenTopo);

        mapController = map.getController();
        mapController.setZoom(11.0);

        //we add default zoom buttons, and ability to zoom with 2 fingers (multi-touch)
//        map.setBuiltInZoomControls(true);
        map.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.ALWAYS);
        map.setMultiTouchControls(true);
        ...///...

And if I use the provider for offline (OfflineTileProvider) it only loads the file that I indicate in the constructor (e.g. spain.sqlite), but not another one that may be necessary (e.g. portugal.sqlite), not even the one that is in the cache.


    private void loadOfflineMap(){
        if (!IS_THERE_SOME_MAP_FILE_DOWNLOADED){
            Toast.makeText (this,"No existen mapas descargados.\nProceda a descargar un mapa para poder usar la aplicación Offline",Toast.LENGTH_LONG).show ();
            return;
        }
        File f = new File(basePathMapTileProvider + "spain.sqlite");
        OfflineTileProvider m_OfflineTileProvider = null;
        try {
            m_OfflineTileProvider = new OfflineTileProvider (
                    new SimpleRegisterReceiver (this), new File[] { f });
        } catch (Exception ex) {
            ex.printStackTrace ();
        }
        map.setTileProvider(m_OfflineTileProvider);

        IArchiveFile[] archives = m_OfflineTileProvider.getArchives ();
        if (archives.length > 0){
            String[] tileSources = archives[0].getTileSources ().toArray (new String[0]);
            map.setUseDataConnection (false);
            ITileSource tilesource = FileBasedTileSource.getSource(tileSources[0]);
            map.setTileSource(tilesource);

        }
        ...///...

     }

The question is: How can I do that, in case of losing the connection or not having it, the necessary map stored in memory is automatically loaded?

0 Answers0